美文网首页
mongdb基础查询语法

mongdb基础查询语法

作者: ting723 | 来源:发表于2018-04-18 11:55 被阅读48次

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

  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基础查询语法

    本文是参考本文末尾引用的文章,并加以整理修改而成 查询全部 与SQL对比: 根据条件 = 查询 与SQL对比: 多...

  • 基础查询

    1. 数据查询语句的基础语法 1.1 基础语法 在MySQL中 select 表示查询,select可以单独成句,...

  • 数据查询语句的基础语法

    1. 数据查询语句的基础语法 1.1 基础语法 在MySQL中 select 表示查询,select可以单独成句,...

  • 第3章 基础查询语法

    1. 数据查询语句的基础语法 1.1 基础语法 在MySQL中 select 表示查询,select可以单独成句,...

  • mongdb 高级查询

    mongdb 高级查询 条件查询 (>) 大于 - $gt (<) 小于 - $lt (>=) 大于等于 - $g...

  • 【SQL.基础构建-第二节(2/4)】

    -- Tips:查询基础 --一、SELECT 语句基础 --1.查询指定列:SELECT 关键字 --语法: ...

  • mongo二2018-06-29

    MongoDB基础语法——查询数据 基本查询 find([{文档条件}]):全集合查询 findOne([{文档条...

  • 2021-02-03 DQL查询表中的记录

    select * from 表名; 1. 语法: 2. 基础查询 3. 条件查询

  • MySQL中的DQL语言2019-08-05

    DQL是Data QueryLanguage 的简写,译为:数据查询语言。 一、基础查询 语法: SELECT ...

  • 数据库学习笔记-基础查询

    基础查询 查询字段语法 查询单字段SELECT 字段 FROM 表名;查询多字段SELECT 字段1,字段2,字段...

网友评论

      本文标题:mongdb基础查询语法

      本文链接:https://www.haomeiwen.com/subject/jndvkftx.html