1. 查询全部

1
db.users.find()

与SQL对比:

1
select * from users  

2. 根据条件 = 查询

1
db.users.find({"age" : 27}) 

与SQL对比:

1
select * from users where age = 27 

3. 多条件查询 and

1
db.users.find({"username":"joe","age":27})

与SQL对比:

1
select * from users where useranem = 'joe' and age = 27

4. 返回部分查询结果

1
2
db.users.find({},{"username":1,"email":1})
db.users.find({},{"username":1,"email":1,"_id":0})

1表示返回该字段,0表示不返回,_id如果不表示的话,默认返回

与SQL对比:

1
select username,email from users

5. 查询条件大于小于 >,<

1
db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})

与SQL对比:

1
select * from users where age >=18 and age <= 30 

6. 查询条件不等于 !=

1
b.users.find({"username" : {"$ne" : "joe"}}) 

与SQL对比:

1
select * from users where username <> "joe"

7. 查询条件在范围内 in

1
db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}}) 

与SQL对比:

1
select * from users where ticket_no in (725, 542, 390)  

8. 查询条件不在范围内 not in

1
db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}})

与SQL对比:

1
select * from users where ticket_no not in (725, 542, 390)  

9. 查询条件or

1
db.users.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]})   

与SQL对比:

1
select * form users where ticket_no = 725 or winner = true

10. 查询条件求余

1
db.users.find({"id_num" : {"$mod" : [5, 1]}}) 

与SQL对比:

1
select * from users where (id_num mod 5) = 1  

11. 非查询 not

1
db.users.find({"$not": {"age" : 27}}) 

与SQL对比:

1
select * from users where not (age = 27) 

12. 查询包含username,且username=null

1
2
db.users.find({"username" : {"$in" : [null], "$exists" : true}}) 
// 如果直接通过find({"username" : null})进行查询,那么连带"没有username"的纪录一并筛选出来

与SQL对比:

1
select * from users where username is null 

13. 正则查询

1
db.users.find({"name" : /joey?/i})

与SQL对比:
无对应,与like部分对应

14. 对数组查询

1
2
db.food.find({fruit : {$all : ["apple", "banana"]}}) 
// 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录

与SQL对比:
无对应

15. 查询字段fruit数组中序号为2的值,序号从0开始

1
2
db.food.find({"fruit.2" : "peach"}) 
// 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录

与SQL对比:
无对应

16. 数组元素个数判断

1
db.food.find({"fruit" : {"$size" : 3}}) // 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用  

与SQL对比:
无对应

17. 数组切片查询,仅返回前10个

1
db.users.findOne(criteria, {"comments" : {"$slice" : 10}}) // 对数组的查询,只返回数组comments中的前十条,还可以{"$slice" : -10}, {"$slice" : [23, 10]}; 分别返回最后10条,和中间10条  

与SQL对比:
无对应

18. 嵌套查询

1
db.people.find({"name.first" : "Joe", "name.last" : "Schmoe"})  // 嵌套查询  

与SQL对比:
无对应

19. 数组嵌套查询

1
2
db.blog.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}) 
// 嵌套查询,仅当嵌套的元素是数组时使用,

与SQL对比:
无对应

20. 复杂查询

1
2
db.foo.find({"$where" : "this.x + this.y == 10"}) 
// 复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where

与SQL对比:
无对应

21. $where 支持js函数做查询条件

1
db.foo.find({"$where" : "function() { return this.x + this.y == 10; }"}) // $where可以支持javascript函数作为查询条件  

与SQL对比:
无对应

22. 排序及分页

1
2
db.foo.find().sort({"x" : 1}).limit(1).skip(10); 
// 返回第(10, 11]条,按"x"进行排序,1为正序,-1为倒序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number

与SQL对比:

1
select * from foo order by x asc limit 0,10