# 查询 $ db.person.find() # 相当于:select* from userInfo;默认每页显示20条文档,使用DBQuery.shellBatchSize= 50重新设置 $ db.person.find({"name":"zhangsan"}) # select * from person where name = ‘zhangsan’ # "$gt", "$gte", "$lt", "$lte", "$ne" $ db.person.find({"age":{$gt: 22}}) # select * from person where age > 22 $ db.person.find({"name": /zhang/}) # select * from person where name like '%zhang%' $ db.person.find({},{id:1,name:1}) # select id ,name from person 1:表示显示(true),0:表示不显示 $ db.person.find().sort({"age":1}) # select * from person order by age 1:升序,-1:降序 $ db.person.find().limit(2) # select * from person limit 2 $ db.person.find().skip(5) # select * from person where id not in (select top 5 id from person) $ db.person.find().limit(10).skip(5) # select * from person limit 5,5 用于分页查询 $ db.person.find({name:{$in:["zhangsan","haha"]}}) # select * from person where name in ("zhansgan","haha") $ db.person.findOne() #查询第一条文档 = db.person.find().limit(1) # select * from person limit 0,1 $ db.person.count() #总记录数 = db.person.find().count() $ db.person.find({address:{$exits:true}}) #存在字段address字段的文档
# 修改 $ db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true); # update users set name = ‘changeName’ where age = 25; $ db.users.update({name: 'Lisi'}, {$inc: {age: 10}}, false, true) # update users set age = age + 50 where name = ‘Lisi’ $ db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true) # update users set age = age + 50, name = ‘hoho’ where name = ‘Lisi’