1、查询所有记录
db.userInfo.find();
相当于:select* from userInfo;

2、查询去掉后的当前聚集集合中的某列的重复数据
db.userInfo.distinct("name");
会过滤掉 name 中的相同数据
相当于:select distict name from userInfo;

3、查询 age = 22 的记录
db.userInfo.find({"age": 22});
相当于: select * from userInfo where age = 22;

4、查询 age > 22 的记录
db.userInfo.find({age: {$gt: 22}});
相当于:select * from userInfo where age >22;

5、查询 age < 22 的记录
db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age <22;

6、查询 age >= 25 的记录
db.userInfo.find({age: {$gte: 25}});
相当于:select * from userInfo where age >= 25;

7、查询 age <= 25 的记录
db.userInfo.find({age: {$lte: 25}});

8、查询 age >= 23 并且 age <= 26 注意书写格式
db.userInfo.find({age: {$gte: 23, $lte: 26}});

9、查询 name 中包含 mongo 的数据 模糊查询用于搜索
db.userInfo.find({name: /mongo/});
//相当于%%
select * from userInfo where name like ‘%mongo%’;

10、查询 name 中以 mongo 开头的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%’;

11、查询指定列 name、age 数据
db.userInfo.find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;
当然 name 也可以用 true 或 false,当用 ture 的情况下河 name:1 效果一样,如果用 false 就
是排除 name,显示 name 以外的列信息。

12、查询指定列 name、age 数据, age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from userInfo where age >25;

13、按照年龄排序 1 升序 -1 降序
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});

14、查询 name = zhangsan, age = 22 的数据
db.userInfo.find({name: 'zhangsan', age: 22});
相当于:select * from userInfo where name = ‘zhangsan’ and age = ‘22’;

15、查询前 5 条数据
db.userInfo.find().limit(5);
相当于:selecttop 5 * from userInfo;

16、查询 10 条以后的数据
db.userInfo.find().skip(10);
相当于:select * from userInfo where id not in (
selecttop 10 * from userInfo
);

17、查询在 5-10 之间的数据
db.userInfo.find().limit(10).skip(5);
可用于分页,limit 是 pageSize,skip 是第几页*pageSize

18、or 与 查询
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;

19、findOne 查询第一条数据
db.userInfo.findOne();
相当于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);

20、查询某个结果集的记录条数 统计数量
db.userInfo.find({age: {$gte: 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
如果要返回限制之后的记录数量,要使用 count(true)或者 count(非 0)
db.users.find().skip(10).limit(5).count(true);

mongodb常用查询语句的更多相关文章

  1. mongodb常用查询语句(转)

    1.查询所有记录 db.userInfo.find();相当于:select* from userInfo; 2.查询去掉后的当前聚集集合中的某列的重复数据db.userInfo.distinct(& ...

  2. mongodb常用操作语句

    mongodb常用操作语句 A:创建数据表 db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean&g ...

  3. 23个MySQL常用查询语句

    23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...

  4. Hibernate常用查询语句

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

  5. mongodb的查询语句学习摘要

    看了些资料,对应只需要知道怎么查询和使用mongodb的我来说,这些足够啦. 左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * fr ...

  6. MySQL常用查询语句汇总(不定时更新)

    在这篇文章中我会通过一些例子来介绍日常编程中常用的SQL语句   目录: ## 1.数据库的建立     ## 1.数据库的建立   实例将ER图的形式给出:   由此转换的4个关系模式:      ...

  7. mysql—常用查询语句总结

    关于MySQL常用的查询语句 一查询数值型数据: ; 查询谓词:>,=,<,<>,!=,!>,!<,=>,=< 二查询字符串 SELECT * FROM ...

  8. MongoDB常用查询,排序,group,SpringDataMongoDB update group

    MongoDB查询 指定查询并排序 db.getCollection('location').find({"site.id":"川A12345","s ...

  9. MongoDB简单查询语句<平时使用语录,持续更新>

    MongoDB查询语句 --查询近三个月的客户使用量  aggregate:使用聚合  match:过滤  group分组   -- mysql中select org_code as 近三个月使用商户 ...

随机推荐

  1. Python request SSL证书问题

    错误信息如下: 1 requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_s ...

  2. 截取字段split

    172.0.0.1String[] splitAddress=qip.split("\\.");//--172001 String ip=splitAddress[0]+" ...

  3. 解决python tkinter 与 sleep 延迟问题

    多线程(threading——join) join ()方法:主线程A中,创建了子线程B,并且在主线程A中调用了B.join(),那么,主线程A会在调用的地方等待,直到子线程B完成操作后, 才可以接着 ...

  4. phpstudy2018 安装xdebug扩展

    第一步:查看PHP版本信息 第二步:到xdebug下载页去下载最新的版本(注意:要下载PHP对应版本) 第三步:把扩展php_xdebug-2.7.0alpha1-7.2-vc15-nts.dll放到 ...

  5. btcpool之BlockMaker

    一.简介 BlockServer将StratumServer发送的solvedshare数据(块头和coinbase交易)与GbtMaker发送的rawgbt数据(其他交易)一起组装成一个块,然后通过 ...

  6. P3803 【模板】多项式乘法(FFT)

    传送门: 参考博客 1:大佬  attack 参考博客 2:大佬  胡小兔 在这里再膜拜一下这两位大佬 Orz%%% #include<iostream> #include<cstd ...

  7. JAVA判断是否是手机设备访问

    package com.common.util; import java.util.regex.Matcher;import java.util.regex.Pattern; /** * 检测是否为移 ...

  8. 基数排序-LSD

    这个最低位优先的基数排序,非常适合移植到硬件设备中,所以,我们提供一个C源码 —————————————————————————————————————— #include <stdio.h&g ...

  9. leafLet入门教程兼leafLet API中文文档参考

    英文文档参考:https://leafletjs.com/reference-1.3.4.html#popup 博客参考:https://blog.csdn.net/qq_36595013/artic ...

  10. Mybatis框架(未完待续)

    1.框架概述:                        它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题.好处:框架封装了很多的细节,使开发者可以使用极简的方式实现功能.大大提高开 ...