日期:2014-05-16  浏览次数:20444 次

mongodb一些常用query命令

mongoDB的备份

./mongodump -h 127.0.0.1? -d dbname -c collectionName -o bakDir



mongoDB的query语句:

?

select:

?

????? db.users.find({})
?
? ? ? db.users.find({'last_name': 'Smith'})
?
?field selection:
?
? ? //只取ssn? 相当于 select ssn from db where last_name == 'Smith'
??? // retrieve ssn field for documents where last_name == 'Smith':
??? db.users.find({last_name: 'Smith'}, {'ssn': 1});

??? //除了域 thumbnail ,其他的域都取
??? // retrieve all fields *except* the thumbnail field, for all documents:
??? db.users.find({}, {thumbnail:0});

?

?

Sorting

MongoDB queries can return sorted results. To return all documents and sort by last name in ascending order, we'd query like so:

  db.users.find({}).sort({last_name: 1});
{last_name: 1} 升序    
{last_name: 0} 降序

Skip and Limit

MongoDB also supports skip and limit for easy paging. Here we skip the first 20 last names, and limit our result set to 10:

db.users.find().skip(20).limit(10);
db.users.find({}, {}, 10, 20); // same as above, but less clear


?

?