1、查询所有记录

db.userInfo.find();

相当于:

select* from userInfo;

默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”
但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize= 50;这样每页就显示50条记录了。

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}});

相当于:

select * from userInfo where age <= 25;

8、查询 age >= 23 并且 age <= 25 注意书写格式

db.userInfo.find({age: {$gte: 23, $lte: 25}});

相当于:

select * from userInfo where age>=23 and age <= 25;

9、查询 age != 25 的记录

db.userInfo.find({age: {$ne: 25}});
相当于:

select * from userInfo where age != 25;

10、查询 name 中包含 mongo 的数据 模糊查询用于搜索

db.userInfo.find({name: /mongo/});

相当于:

select * from userInfo where name like '%mongo%';

11、查询 name 中以 mongo 开头的

db.userInfo.find({name: /^mongo/});

相当于:

select * from userInfo where name like 'mongo%';

12、查询 name 中以 mongo 结尾的

db.userInfo.find({name: /mongo$/});

相当于:

select * from userInfo where name like ‘%mongo’;

模糊查询语法:{ : /pattern/ }
其中options值可以为:

i -- 不区分大小写。
m -- 匹配value中有换行符(\n)的情形,还有一个情形是:匹配规则中使用了锚,所谓的锚就是^ 开头, $ 结尾。
s -- 允许点字符(.)匹配所有的字符,包括换行符。
x -- 忽视所有空白字符。

13、查询指定列 name、age 数据

db.userInfo.find({}, {name: 1, age: 1});

相当于:

select name, age from userInfo;

当然 name 也可以用 true 或 false,当用 ture 的情况下和 name:1 效果一样,如果用 false 就是排除 name,显示 name 以外的列信息。

14、查询指定列 name、age 数据, age > 25

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

相当于:

select name, age from userInfo where age > 25;

15、按照年龄排序 1 升序 -1 降序
升序:

db.userInfo.find().sort({age: 1});

相当于:

select * from userInfo order by age asc;

降序:

db.userInfo.find().sort({age: -1});

相当于:

select * from userInfo order by age desc;

17、查询前 5 条数据

db.userInfo.find().limit(5);

相当于:

select * from userInfo limit 5;

18、查询 10 条以后的数据

db.userInfo.find().skip(10);

19、查询在 6-10条 之间的数据

db.userInfo.find().limit(10).skip(5);

可用于分页,limit 是 pageSize,第n页时 skip 是 (n-1)*pageSize
相当于:

select * from userInfo limit 5,5;

20、and 查询 name = zhangsan, age = 22 的数据

db.userInfo.find({name: 'zhangsan', age: 22});

相当于:

select * from userInfo where name = 'zhangsan' and age = 22;

21、or 查询

db.userInfo.find({$or: [{age: 22}, {age: 25}]});

相当于:

select * from userInfo where age = 22 or age = 25;

注意多条件间用中括号[]包围。

22、in 查询

db.userInfo.find({age :{$in:[22,25]}});

相当于:

select * from userInfo where age in (22,25);

23、查询某个结果集的记录条数 统计数量

db.userInfo.find({age: {$gte: 25}}).count();

相当于:

select count(*) from userInfo where age >= 20;

skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是先 sort(), 然后是 skip(),最后是显示的 limit()。

24、查询某个时间段的数据(时间为日期类型,非字符串类型)

db.userInfo.find({createTime:{$gt:ISODate("2020-11-09T00:00:00Z")}});

相当于:

select * from userInfo where createTime> '2020-11-09 00:00:00';

25、对表中一字段进行统计求和

db.userInfo.aggregate({$group:{_id:null,score:{$sum:"$score"}}})

相当于:

SELECT SUM(score) from userInfo;

26、对表中一字段进行统计求平均值

db.userInfo.aggregate({$group:{_id:null,score:{$avg:"$score"}}})

相当于:

SELECT AVG(score) from userInfo;

27、对表中指定条件记录中的某字段求和

db.userInfo.aggregate({$match:{createTime:{$gte:ISODate("2020-11-10T00:00:00Z"),$lt:ISODate("2020-11-11T00:00:00Z")}}},{$group:{_id:null,score:{$sum:"$score"}}})

相当于:

SELECT SUM(score) from userInfo where createTime >= '2020-11-10 00:00:00' and createTime < '2020-11-11 00:00:00';

28、根据A表,匹配B表所有满足条件的集合,如根据用户表userInfo表中的userId字段找出userAdress表中所有地址的集合,其中userId也为userAdress中的字段。

假设 有 用户集合, 存储的测试数据 如下:

db.userInfo.insert([
{ "_id" : 1, "userId" : "xxxx", "username" : "ruink", "website" : "www.51ste.com" },
{ "_id" : 2, "userId" : "yyyy", "username" : "foosingy", "website" : "www.scgossip.com" }
])
假设 有 地址集合, 存储的测试数据 如下:

db.userAdress.insert([
{ "_id" : 1, "userId" : "xxxx", address: "测试地址1"},
{ "_id" : 2, "userId" : "yyyy", address: "测试地址2"},
{ "_id" : 3, "userId" : "xxxx", address: "测试地址3"},
])

查询语句:

上表为找出userId="xxxx"的所有地址的集合,查询结果如下:

复制

注意:

字段是什么类型,那么查询时字段值的类型就应该是什么类型,比如如果字段类型是 NumberLog,那么查询时就应该执行查询类型为 NumberLog,如

转自:https://blog.csdn.net/qq_41767116/article/details/125586683

mangodb查询语句的更多相关文章

  1. SQL Server-简单查询语句,疑惑篇(三)

    前言 对于一些原理性文章园中已有大量的文章尤其是关于索引这一块,我也是花费大量时间去学习,对于了解索引原理对于后续理解查询计划和性能调优有很大的帮助,而我们只是一些内容进行概括和总结,这一节我们开始正 ...

  2. thinkphp中的查询语句

    <?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller { ...

  3. Oracle 查询语句(where,order by ,like,in,distinct)

    select * from production;alter table production add productionprice number(7,2); UPDATE production s ...

  4. 45 个非常有用的 Oracle 查询语句

    ​ 这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 Oracle 开发者都必备的技能,所以快 ...

  5. mysql查询语句select-子查询

    1 子查询定义 在一个表表达中可以调用另一个表表达式,这个被调用的表表达式叫做子查询(subquery),我么也称作子选择(subselect)或内嵌选择(inner select).子查询的结果传递 ...

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

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

  7. SqlServer 一个查询语句以致tempdb增大55G (转载)

    SqlServer 一个查询语句导致tempdb增大55G 今天操作着服务器,突然右下角提示“C盘空间不足”! 吓一跳!~ 看看C盘,还有7M!!!这么大的C盘空间怎么会没了呢?搞不好等下服务器会动不 ...

  8. ThinkPHP(3)SQL查询语句

    ThinkPHP中对查询语句,包含了基本的查询方式.表达方式.快速查询.区间查询.组合查询.SQL查询.动态查询和子查询. 一.查询方式 ThinkPHP提供了三种基本的查询方式:字符串条件查询.索引 ...

  9. MySQL知识树-查询语句

    在日常的web应用开发过程中,一般会涉及到数据库方面的操作,其中查询又是占绝大部分的.我们不仅要会写查询,最好能系统的学习下与查询相关的知识点,这篇随笔我们就来一起看看MySQL查询知识相关的树是什么 ...

  10. sql查询语句

    //查询表的字段名和字段类型select column_name,data_type from information_schema.columns where table_name = '表名' / ...

随机推荐

  1. D - Pair of Topics

    D - Pair of Topics 思路: 这个题需要一点思路,ai+aj>bi+bj可以转换成ai-bi+aj-bj>0,也就是c[i]=a[i]-b[i],只需要找c[i]+c[j] ...

  2. vue获取标签对象的方式

    我知道2种方式: 1.在标签内 使用 ref 属性定义对象名,使用this.$refs.[name] 调用 2.在标签内 使用 函数传递事件对象, 定义, <div @click="h ...

  3. ts的装饰器

    console.log('装饰器.......') // 装饰器就是一个方法,可以注入到类,方法,属性上来拓展类,属性,方法,参数的功能 // 常见:类装饰器,属性装饰器,方法装饰器,参数装饰器 // ...

  4. 全链路压测SOP

    压测模型构建:人工 线上(大促)流量数据 (数据脱敏) 日常流量数据 业务方新的特性产生的变更数据 友商做过的事情 压测模型构建:自动 流程包括:录制-清洗-回放 (目前能做好的公司非常少) 压测标准 ...

  5. jdbc与Statement接口

    Statement接口引入 使用Statement接口实现添加数据操作 使用Statement接口实现更新数据操作 使用Statement接口实现删除数据操作

  6. 【小记】如果 golang 内存不够了怎么办

    在看 redis 1.0 源码时,总会看到需要申请内存的地方,如果申请不到需要大的内存就会返回 NULL,然后在调用层抛出 oom. 比如 listDup 中在复制特殊 value 或者加入新节点时都 ...

  7. Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorlist:...

    大家都知道Centos8于2021年年底停止了服务,大家再在使用yum源安装时候,出现下面错误"错误:Failed to download metadata for repo 'AppStr ...

  8. vl_nnconv.mexw64找不到指定模块

    在Installing and compiling the library--matconvnet-1.0-beta25时遇到的错误,总是提示"Invalid MEX-file 'C:\Us ...

  9. 【Python】语言的控制流程

    二.Python语言的控制流程 1.程序的组织结构 1.1 顺序结构:从上到下顺序的执行代码,直到程序结束,没有任何判断和跳转 1.2 选择结构:根据判断条件的布尔值选择性的执行部分代码:if语句 1 ...

  10. maven profile 的作用

    maven 的profile  可以是我们通过编译时指定 -P 来实现 使用不同的 属性变量. 比如: <profiles> <profile> <id>local ...