mongdb基础查询语法

本文是参考本文末尾引用的文章,并加以整理修改而成

  1. 查询全部
db.users.find()

与SQL对比:

 select * from users  
  1. 根据条件 = 查询
db.users.find({"age" : 27}) 

与SQL对比:

select * from users where age = 27 
  1. 多条件查询 and
db.users.find({"username":"joe","age":27})

与SQL对比:

 select * from users where useranem = 'joe' and age = 27
  1. 返回部分查询结果
db.users.find({},{"username":1,"email":1})
db.users.find({},{"username":1,"email":1,"_id":0})

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

与SQL对比:

 select username,email from users
  1. 查询条件大于小于 >,<
db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})

与SQL对比:

 select * from users where age >=18 and age <= 30 
  1. 查询条件不等于 !=
b.users.find({"username" : {"$ne" : "joe"}}) 

与SQL对比:

select * from users where username <> "joe"
  1. 查询条件在范围内 in
db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}}) 

与SQL对比:

select * from users where ticket_no in (725, 542, 390)  
  1. 查询条件不在范围内 not in
db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}})

与SQL对比:

 select * from users where ticket_no not in (725, 542, 390)  
  1. 查询条件or
db.users.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]})   

与SQL对比:

select * form users where ticket_no = 725 or winner = true
  1. 查询条件求余
db.users.find({"id_num" : {"$mod" : [5, 1]}}) 

与SQL对比:

select * from users where (id_num mod 5) = 1  
  1. 非查询 not
db.users.find({"$not": {"age" : 27}}) 

与SQL对比:

select * from users where not (age = 27) 
  1. 查询包含username,且username=null
db.users.find({"username" : {"$in" : [null], "$exists" : true}}) 
// 如果直接通过find({"username" : null})进行查询,那么连带"没有username"的纪录一并筛选出来  

与SQL对比:

select * from users where username is null 
  1. 正则查询
db.users.find({"name" : /joey?/i})

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

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

与SQL对比:
无对应

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

与SQL对比:
无对应

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

与SQL对比:
无对应

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

与SQL对比:
无对应

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

与SQL对比:
无对应

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

与SQL对比:
无对应

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

与SQL对比:
无对应

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

与SQL对比:
无对应

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

与SQL对比:

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

引用参考:
mongo数据库的各种查询语句示例(比较全的)

你可能感兴趣的:(mongdb基础查询语法)