自己整理了一些mongoDB日常会用到的语句,
sql | mongo |
select * from lhc | db.lhc.find() |
select * from lhc limit 1 | db.lhc.findOne() db.lhc.find().limit(1) |
select id,name from lhc | db.lhc.find({},{"id":1,"name":1}) |
select * from lhc where age=33 | db.lhc.find({"age":33}) |
select * from lhc where age=33 and name='luo huichao' | db.lhc.find({"age":33,"name":"luo huichao"}) |
select * from lhc where age=33 or age=50 | db.lhc.find({"$or":[{"age":33},{"age":50}]}) |
select id,name from lhc where age=33 | db.lhc.find({"age":33},{"id":1,"name":1}) |
select * from lhc where age=33 order by name | db.lhc.find({"age":33}).sort({"name":1}) |
select * from lhc where age>33 | db.lhc.find({"age":{"$gt":33}}) db.lhc.find({"age":{"$ge":33}}) >= db.lhc.find({"age":{"$le":33}})<= |
select * from lhc where age>=33 and age<50 | db.lhc.find({"age":{"$ge":33,"$lt":50}}) |
select * from lhc where age!=33 | db.lhc.find({"age":{"$ne":33}}) db.lhc.find({"age":{"$eq":33}})= |
select * from lhc where name like '%luo%' | db.lhc.find({"name":/luo/}) db.lhc.find({"name":/^luo/}) like 'luo% |
select * from lhc where age in(33,44) | db.lhc.find({"age":{"$in":[33,44]}}) db.lhc.find({"age":{"$nin":[33,44]}}) not in |
select distinct name from lhc | db.lhc.distinct("name") |
select * from lhc where name is null | db.lhc.find({"name":{"$exists":false}}) |
如果有补充,请在下面顶上!!!