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

mongo数据库的各种查询语句示例的更多相关文章

  1. sql查询语句示例

    今天没事又专门学习了一下sql查询语句,个人感觉太重要了,于是就找了网上的一个示例自己练了起来,感觉学到了很多,下面跟大家分享一下sql查询语句的示例操作. 首先,我建了5张表,分别如下: (a)学生 ...

  2. MYSQL数据库中的查询语句

    查询的方法 *简单查询:select * from 表名 (* = 所有的) *读取特定列:select 字段一,字段二 from 表名 *条件查询:select * from 表名 where (多 ...

  3. solr查询语句示例

    url示例:sort=id+desc&&fq=date_time:[20081001 TO 20091031]&wt=json&json.nl=map&q=st ...

  4. 50条SQL查询技巧、查询语句示例

    学习了 1.查询“001”课程比“002”课程成绩高的所有学生的学号: 2.查询平均成绩大于60分的同学的学号和平均成绩: 3.查询所有同学的学号.姓名.选课数.总成绩: 4.查询姓“李”的老师的个数 ...

  5. oracle数据库重要的查询语句

    查看所有数据文件(dbf文件)的存放位置 SQL> select name from v$datafile; 标红色的为默认表空间文件 SQL> select name from v$da ...

  6. mysql连接查询语句示例

    eg: 内连接:     select student.*,grade.* from student join grade where student.sid=grade.sid;     selec ...

  7. Datawhale MySQL 训练营 Task2 查询语句

    目录 MySQL 管理 MySQL 用户管理 参考 数据库管理 SQ查询语句 1. 导入示例数据库,教程 MySQL导入示例数据库 2. 查询语句 SELECT 3. 筛选语句 WHERE ,过滤 4 ...

  8. oracle数据库中的基本语句

    下面的都是最基本的oracle数据库的数据查询语句,这是我在网上整理的一份文档,方便以后自己的查看,当然,能把这些记下来就是最好的. 说明:查询表中的数据 1. select * from emp; ...

  9. Hibernate常用查询语句

    Hibernate常用查询语句 Hib的检索方式1'导航对象图检索方式.通过已经加载的对象,调用.iterator()方法可以得到order对象如果是首次执行此方法,Hib会从数据库加载关联的orde ...

随机推荐

  1. Windows命令查看文件MD5,SHA1,SHA256 文件校验

    certutil -hashfile yourfilename.ext MD5 certutil -hashfile yourfilename.ext SHA1 certutil -hashfile ...

  2. 将List的元素通过中文字符串排序

    类customer public class Customer { public String name; public int age; Customer(String name, int age) ...

  3. NoClassDefFound Error: com/fasterxml/jackson/annotation/JsonAutoDetect

    少了 jackson-annotation https://blog.csdn.net/qq_36497454/article/details/80461676

  4. 同步&异步+阻塞&非阻塞(理解)

    0 - 同步&异步 同步和异步关注的是消息通信机制. 0.1 - 同步 由“调用者”主动等待这个“调用”结果.即是,发出一个“调用”时,在没有得到结果之前,该“调用”不返回,一旦调用返回,则得 ...

  5. vue 学习笔记—Resource

    1.首先是引入  或者用npm来安装  cnpm i vue-resource --save(推荐) 3.提供的api 关于请求写法: get(){ // get请求 this.$http.get( ...

  6. Zookeeper客户端Curator基本API

    在使用zookeper的时候一般不使用原生的API,Curator,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连.反复注册Watcher和NodeExistsExceptio ...

  7. 序列化 java.io.Serializable

    1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态,并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存Object States,但是Java给你提供一种应该比 ...

  8. Android软键盘在清单文件中所有配置含义

    android:windowSoftInputMode 活动的主窗口如何与包含屏幕上的软键盘窗口交互.这个属性的设置将会影响两件事情: 1>     软键盘的状态——是否它是隐藏或显示——当活动 ...

  9. CVE-2019-0797漏洞:Windows操作系统中的新零日在攻击中被利用

    https://securelist.com/cve-2019-0797-zero-day-vulnerability/89885/ 前言 在2019年2月,卡巴实验室的自动漏洞防护(AEP)系统检测 ...

  10. eMMC基础技术4:eMMC command

    1.前言 本文主要对eMMC的command进行详细介绍,主要包含如下内容: (1)command类型 (2)command格式 2.command类型 command类型 说明 bc 不带respo ...