mangodb查询语句
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查询语句的更多相关文章
- SQL Server-简单查询语句,疑惑篇(三)
前言 对于一些原理性文章园中已有大量的文章尤其是关于索引这一块,我也是花费大量时间去学习,对于了解索引原理对于后续理解查询计划和性能调优有很大的帮助,而我们只是一些内容进行概括和总结,这一节我们开始正 ...
- thinkphp中的查询语句
<?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller { ...
- Oracle 查询语句(where,order by ,like,in,distinct)
select * from production;alter table production add productionprice number(7,2); UPDATE production s ...
- 45 个非常有用的 Oracle 查询语句
这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 Oracle 开发者都必备的技能,所以快 ...
- mysql查询语句select-子查询
1 子查询定义 在一个表表达中可以调用另一个表表达式,这个被调用的表表达式叫做子查询(subquery),我么也称作子选择(subselect)或内嵌选择(inner select).子查询的结果传递 ...
- mongodb的查询语句学习摘要
看了些资料,对应只需要知道怎么查询和使用mongodb的我来说,这些足够啦. 左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * fr ...
- SqlServer 一个查询语句以致tempdb增大55G (转载)
SqlServer 一个查询语句导致tempdb增大55G 今天操作着服务器,突然右下角提示“C盘空间不足”! 吓一跳!~ 看看C盘,还有7M!!!这么大的C盘空间怎么会没了呢?搞不好等下服务器会动不 ...
- ThinkPHP(3)SQL查询语句
ThinkPHP中对查询语句,包含了基本的查询方式.表达方式.快速查询.区间查询.组合查询.SQL查询.动态查询和子查询. 一.查询方式 ThinkPHP提供了三种基本的查询方式:字符串条件查询.索引 ...
- MySQL知识树-查询语句
在日常的web应用开发过程中,一般会涉及到数据库方面的操作,其中查询又是占绝大部分的.我们不仅要会写查询,最好能系统的学习下与查询相关的知识点,这篇随笔我们就来一起看看MySQL查询知识相关的树是什么 ...
- sql查询语句
//查询表的字段名和字段类型select column_name,data_type from information_schema.columns where table_name = '表名' / ...
随机推荐
- 推荐2个可用于毕设的微信小程序
智能垃圾回收小程序 下载: http://market.zhenzikj.com/detail/103.html 2. 通用答题小程序 下载http://market.zhenzikj.com/det ...
- Object.create() 方浅析
Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. Object.create(proto[, propertiesObject]) 参数 pro ...
- win10系统如何安装无线网卡驱动?win10系统安装无线网卡驱动教程
转载:win10系统如何安装无线网卡驱动?win10系统安装无线网卡驱动教程_windows10_Windows系列_操作系统_脚本之家 (jb51.net) win10系统如何安装无线网卡驱动? 有 ...
- Java-Collectors.groupingBy
Java中的Collectors类的groupingBy()方法用于按某些属性对对象进行分组并将结果存储在Map实例中. 当我我们想利用它的特性,我们需要指定一个属性来执行分组.此方法提供的函数类似于 ...
- kotlin information
Kotlin常用的by lazy你真的了解吗 => https://juejin.cn/post/7057675598671380493 END
- 1168 Prime Day(20)
The above picture is from Sina Weibo, showing May 23rd, 2019 as a very cool "Prime Day". T ...
- 《Rust权威指南》学习笔记——4. 认识所有权
Rust权威指南学习笔记--认识所有权 什么是所有权 1. 所有权规则 Rust中的每一个值都有一个对应的变量作为它的所有者. 在同一时间内,值有且仅有一个所有者. 当所有者离开自己的作用域时,它持有 ...
- shell—if + case条件语句
if 条件语句 1. 概述 在shell的各种条件结构和流程控制结构中都要进行各种测试,然后根据测试结果执行不同的操作,有时候也会与 if 等条件语句相结合,来完成测试判断,以减少程序运行错误. 2. ...
- mysql in和find_in_set
一.查询包含","的列 1.如果查询条件包含单引号 用in 如:select * from t_test where names in ('李红'); 只能查询出names列中值为 ...
- color-color diagram data