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. 严重:one or more listeners failed. Full details will be found in the appropriate container log file

    one or more listeners failed. Full details will be found in the appropriate container log file   这句话 ...

  2. Typescript基础

    参数类型新特性: ​类型声明 变量/函数:类型(string.any.number.boolean.void五种基本类型) 例如:var age:number=19; function test(na ...

  3. springBoot生成日志文件

    一.安装lombok 说明: 安装bomlok后model可以不用写get.set方法,slf4j日志直接使用log打印 1. Maven Repository中下载lombok.jar 2. 将lo ...

  4. vue-cli 创建项目失败

    vue-cli 创建一个vue项目报错 npm code 404. 尝试解决方法都有,我是第3种方法生效 更新npm 重新安装nodejs vue-cli@3.0.1 在创建项目的时候,不能开启其他v ...

  5. ES6的Proxy和Reflect

    Proxy 有一个原始的数据对象,通过代理出来一个新的对象,用户操作的是这个新的对象 { let obj ={ time:'2018-01-01', name:'lx' , _r:123 } let ...

  6. JAVA学习笔记 (okHttp3的用法)

    最近的项目中有个接口是返回文件流数据,根据我们这边一个验签的插件,我发现里面有okHttpClient提供了Call.Factory,所以就学习了下okHttp3的用法. 1.概述 okhttp是一个 ...

  7. MySQL使用root权限创建用户并授权

    MySql篇 1.下载并安装Mysql (1)下载地址 MySQL-8.0下载地址 (2)Mysql配置 1.home目录下命令行执行:vi    .bash_profile来配置MySql绝对路径 ...

  8. 2019春第六周作业Compile Summarize

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 在这里 我在这个课程的目标是 能够熟练掌握指针的用法 这个作业在那个具体方面帮助我实现目标 对指针的使用更加得心应手 参考文献与网址 C语 ...

  9. vue中动态样式不起作用? scoped了解一下

    vue中style标签使用属性scoped的注意事项 style上添加属性scoped可以实现样式私有化,但是在使用动态样式时,样式会不起作用.可以先去掉scoped

  10. PyAutoGUI——让所有GUI都自动化

    2015-08-17:输入中文bug没有解决,目前的解决方案是Python 2.X环境下安装pyperclip和pyautogui,用复制粘贴来实现. In [ ]: import pyperclip ...