1、db.users.find()
-- db.getCollection('users').find({})
-- select * from users
2、db.users.find({"age" : 27})
-- select * from users where age = 27
3、db.users.find({"username" : "joe", "age" : 27})
-- select * from users where "username" = "joe" and age = 27
4、db.users.find({}, {"username" : 1, "email" : 1})
-- select username, email from users
5、db.users.find({}, {"username" : 1, "_id" : 0})
-- no case
-- 即时加上了列筛选,_id也会返回,必须显式的阻止_id返回
6、db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})
-- select * from users where age >=18 and age <= 30
-- $lt(<) $lte(<=) $gt(>) $gte(>=)
7、db.users.find({"username" : {"$ne" : "joe"}})
-- select * from users where username != "joe"
8、db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}})
-- select * from users where ticket_no in (725, 542, 390)
9、db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}})
-- select * from users where ticket_no not in (725, 542, 390)
10、db.users.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]})
-- select * form users where ticket_no = 725 or winner = true
11、db.users.find({"username" : {"$in" : [null], "$exists" : true}})
-- select * from users where username is null
-- 如果直接通过find({"username" : null})进行查询,那么连带"没有username"的纪录一并筛选出来
12、db.users.find({"username" : {"$nin" : [null]}})
-- db.users.find({"username" : {"$ne" : null}})
-- select * from users where username is not null
mongo数据库的各种查询语句示例
点赞
收藏