0

MongoDB select 操作大全

已有 178 阅读此文人 - - MongoDB,数据库 -
MongoDB select 操作大全
1.WHERE #
select * from users where name = 'user1'
> db.users.find({name:"user1"}) { "_id" : ObjectId("4c4528a0b55f2224d447e4b0"), "name" : "user1", "age" : 21, "sex" : 1}
select * from users where name = 'user1' and age = 21
> db.users.find({name:"user1", age:21}) { "_id" : ObjectId("4c4528a0b55f2224d447e4b0"), "name" : "user1", "age" : 21, "sex" : 1}
2.FIELDS #
select name, age from users where age = 21
> db.users.find({age:21}, {'name':1, 'age':1}) { "_id" : ObjectId("4c452c343d48c8f284b388e0"), "name" : "user1", "age" : 21 }
select name, age from users
> db.users.find({}, {'name':1, 'age':1})
3.SORT #
select * from users order by age
> db.users.find().sort({age:1})
select * from users order by sex asce, age desc
> db.users.find().sort({sex:1, age:-1})
4.SLICE #
select * from users skip 2 limit 3
> db.users.find().skip(2).limit(3)
select * from users where sex = 1 and age > 23 and age < 28
> db.users.find({sex:1, age:{$gt:23, $lt:28}})
比较操作包括:$gt (>)、$lt (<)、$gte (>=)、$lte(<=)、$ne (!=)
5. IN #
select * from users where age in (23, 26, 32)
> db.users.find({age:{$in:[23,26,32]}})
对应的操作符有 $nin (not in)
6.COUNT #
select count(*) from users where age > 30
> db.users.find({age:{$gt:30}}).count()
7.OR #
select * from users where age = 25 or age = 28
> db.users.find({$or:[{age:25}, {age:28}]})
select * from users where age <= 23 or age >= 33
> db.users.find({$or:[{age:{$lte:23}}, {age:{$gte:33}}]})
期待你一针见血的评论,Come on!

不用想啦,马上 "登录"  发表自已的想法.