mongodb与sql语句对比

  1. 左边是mongodb查询语句,右边是sql语句。对照着用,挺方便。
  2. db.users.find() select * from users
  3. db.users.find({"age" : 27}) select * from users where age = 27
  4. db.users.find({"username" : "joe", "age" : 27}) select * from users where "username" = "joe" and age = 27
  5. db.users.find({}, {"username" : 1, "email" : 1}) select username, email from users
  6. db.users.find({}, {"username" : 1, "_id" : 0}) // no case  // 即时加上了列筛选,_id也会返回;必须显式的阻止_id返回
  7. db.users.find({"age" : {"$gte" : 18, "$lte" : 30}}) select * from users where age >=18 and age <= 30 // $lt(<) $lte(<=) $gt(>) $gte(>=)
  8. db.users.find({"username" : {"$ne" : "joe"}}) select * from users where username <> "joe"
  9. db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}}) select * from users where ticket_no in (725, 542, 390)
  10. db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}}) select * from users where ticket_no not in (725, 542, 390)
  11. db.users.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]}) select * form users where ticket_no = 725 or winner = true
  12. db.users.find({"id_num" : {"$mod" : [5, 1]}}) select * from users where (id_num mod 5) = 1
  13. db.users.find({"$not": {"age" : 27}}) select * from users where not (age = 27)
  14. db.users.find({"username" : {"$in" : [null], "$exists" : true}}) select * from users where username is null // 如果直接通过find({"username" : null})进行查询,那么连带"没有username"的纪录一并筛选出来
  15. db.users.find({"name" : /joey?/i}) // 正则查询,value是符合PCRE的表达式
  16. db.food.find({fruit : {$all : ["apple", "banana"]}}) // 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录
  17. db.food.find({"fruit.2" : "peach"}) // 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录
  18. db.food.find({"fruit" : {"$size" : 3}}) // 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用
  19. db.users.findOne(criteria, {"comments" : {"$slice" : 10}}) // 对数组的查询,只返回数组comments中的前十条,还可以{"$slice" : -10}, {"$slice" : [23, 10]}; 分别返回最后10条,和中间10条
  20. db.people.find({"name.first" : "Joe", "name.last" : "Schmoe"})  // 嵌套查询
  21. db.blog.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}) // 嵌套查询,仅当嵌套的元素是数组时使用,
  22. db.foo.find({"$where" : "this.x + this.y == 10"}) // 复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where
  23. db.foo.find({"$where" : "function() { return this.x + this.y == 10; }"}) // $where可以支持javascript函数作为查询条件
  24. db.foo.find().sort({"x" : 1}).limit(1).skip(10); // 返回第(10, 11]条,按"x"进行排序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number
  25. 多条件查询  
    db.getCollection('CollectionName').find({"userId":1},{"customerId":61});
  26. 根据时间戳范围查询   
    db.getCollection('CollectionName').find({"userId":61},{"timestamp":{"$gt":1540449300000,"$lte":1540550100000}})
  27. 条件查排序并分页:1.是升序,  -1是降序    
    db.getCollection('CollectionName').find({"userId":361}).sort({"time":-1}).limit(100);
  28. ISOdate时间范围查询  
    db.getCollection('CollectionName').find({ "timestamp" : { "$gte" : ISODate("2018-04-20T00:00:00Z")
    , "$lt" : ISODate("2018-04-21T00:00:00Z") }});
  29. 使用$and多条件查询    

    db.getCollection('CollectionName').find( {$and:[{"userId":37761},{"domain":"time.com"},{"timestamp":{"$gt":1540483200000,"$lte":1540550100000}}]});

  30. mongodb中对应的范围标识符:

    "$lt"===================>  "<"
    "$lte"==================>  "<="
    "$gt"===================>  ">"
    "$gte"==================>  ">="
    "$ne"===================>  "!="

MongoDB 查询SQL 大全相关推荐

  1. MongoDB查询用法大全

    转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...

  2. mongodb查询大全mongo语句

    mongodb查询大全mongo语句 一.前言 虽然这些语句在开发当中不会使用,因为springdataMongoDB封装的非常完美了.但是这里的语句思想和关系型数据库有些区别,所以需要拿出来详细的讲 ...

  3. Mongodb查询语句与Sql语句对比

    左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * from users db.users.find({"age" ...

  4. SQL 分页查询语句大全即(查找第N到M条记录的方法)

    SQL 分页查询语句大全即(查找第N到M条记录的方法) 第一种方法,我的原创方法 row=2 表示分页行数 page=1 表示页码 getnum=row*page select * from  (se ...

  5. 从SQL过渡至MongoDB查询对照表

    从SQL过渡至MongoDB查询对照表 SQL 说明 Mongo 说明 CREATE TABLE USERS (a Number, b Number) Implicit or use MongoDB: ...

  6. mongodb 跟踪SQL语句及慢查询收集

    有个需求:跟踪mongodb的SQL语句及慢查询收集 第一步:通过mongodb自带函数可以查看在一段时间内DML语句的运行次数. 在bin目录下面运行  ./mongostat -port 端口号  ...

  7. orcal SQL查询语句大全集锦

    orcal   SQL查询语句大全集锦 一. 简单查询   简单的Transact-SQL查询只包括选择列表.FROM子句和WHERE子句.它们分别说明所查询列.查询的 表或视图.以及搜索条件等. 例 ...

  8. mysql的查询语句大全_sql语句(sql数据库查询语句大全)

    sql语句 结构化查询语言(StructuredQueryLanguage)缩写为SQL.结构化查询语言是一种数据库查询和编程语言,用于访问数据以及查询,更新和管理关系数据库系统: 程序功能 创建数据 ...

  9. mongodb不等于某个值_MongoDb进阶实践之四 MongoDB查询命令详述

    一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命名和删除.有了这些基 ...

最新文章

  1. BZOJ3473:字符串(后缀数组,主席树,二分,ST表)
  2. zabbix监控管理维护脚本
  3. jquery 判断数据是否重复
  4. C++:undefined reference to vtable 原因与解决办法 [转]
  5. Log日志分析--awstats
  6. 【渝粤题库】陕西师范大学200301几何学作业(高起本)
  7. 21.和和instance of
  8. leetcode python3 简单题119. Pascal's Triangle II
  9. java并发编程实战电子书,王者笔记!
  10. 网易云音乐的焦虑 暗藏在上市后的首份财报里
  11. 中国互联网十大骨干网有哪些?了解互联网的真实网络结构
  12. 实践php检测图片木马
  13. ubuntu 装机必备软件
  14. 终于试用了64位win7
  15. 计算机打表格图,快速填充/微图表/一秒制作打勾方框
  16. 运动装备什么牌子好?运动装备品牌排行榜推荐
  17. Destoon增加内容页的浏览历史记录
  18. 现流行的第三方库及名称
  19. Win11WSA无法启动的解决办法
  20. Uninformed Students: Student–Teacher Anomaly Detection with Discriminative Latent Embeddings(翻译)

热门文章

  1. Three.js 入门篇-制作3D 动漫角色欣赏
  2. DOG算子--------的特征提取(二)
  3. Linux常用命令、Xshell、MobaXterm使用总结
  4. 单极感应S极霍尔开关AR1231
  5. Struts2被曝远程代码执行漏洞;叮咚买菜抢菜工具;find替代方案…|叨资讯
  6. CSS|文本溢出隐藏
  7. 012.西门子PLC对M440变频器三段速控制
  8. 3分钟了解 WebAssembly
  9. 25条让人哭笑不得的趣味小
  10. 淡黄的炼丹炉(篇一):DELL R720深度学习Server上手熟悉