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

最新文章

  1. java Future 接口介绍
  2. apache如何解决跨域资源访问
  3. JavaScript call
  4. PetaPoco4.0 实体某个字段不赋值会更新成null解决方案
  5. 【腾讯GAD暑期训练营游戏程序班】游戏中的特效系统作业说明文档
  6. load、init和initialize的区别
  7. PyMongo下载及安装
  8. Error: Could not access the Package Manager. Is the system running?
  9. Camera拍照声设定
  10. Eclipse对printf()不能输出到控制台的解决方法
  11. 替代 yunio , 推荐一个国外的云存储(同步盘) wuala(这个网站也挂了)——功能评测非常全面
  12. contentWindow,
  13. cuda学习3-共享内存和同步
  14. 《Python编程从入门到实践》_第四章_操作列表
  15. 联发科Helio P90(mt6779),P70(mt6775),P60(MT6771),P35,P22(MT6762)芯片参数规格
  16. loj#6436. 「PKUSC2018」神仙的游戏(生成函数)
  17. HashMap和Hashtable的同和不同(详细比较)
  18. Linux搭建SVN环境
  19. UI简述
  20. Android ormlite like() function is not working

热门文章

  1. jemter 分布式压测
  2. 前端之Vue day08 Vue3项目搭建、setup、toRefs
  3. 软件工程日报六——TextView和button
  4. uniapp组件监听onShow
  5. Python 使用mysql.connector、pymysql和 MYSQLdb(MysqlClient)操作MySQL数据库
  6. pillow 创建图片并添加一些自定义信息
  7. OCR接口
  8. EMQX 在 Kubernetes 中如何进行优雅升级
  9. Linux系统配置安装jdk
  10. vi 快捷键/ctags