首页 > 数据库 >MongoDB常用28条查询语句

MongoDB常用28条查询语句

时间:2023-06-19 10:45:48浏览次数:50  
标签:语句 name MongoDB age 28 db userInfo find select

MongoDB常用28条查询语句

1查询所有记录

db.userInfo.find();

相当于:

select * from userInfo;

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

2查询去掉后的当前聚集集合中的某列的重复数据

db.userInfo.distinct("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’;

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});
db.userInfo.find().sort({age: -1});

相当于:

select * from userInfo order by age asc;
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;

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

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

相当于:

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

21or 查询

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

相当于:

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

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

22in 查询

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"},
])

查询语句:

db.userInfo.aggregate([
   {
     $lookup:
       {
         from: "userAdress",
         localField: "userId",
         foreignField: "userId",
         as: "address_detail"
       }
  },
  { $match : {"userId" :"xxxx"} }
])

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

[
  {
    _id: 1,
    userId: 'xxxx',
    username: 'ruink',
    website:'www.51ste.com',
    address_docs: [
      {
        _id: 1,
        userId: 'xxxx',
        address: '测试地址1'
      },
      {
        _id: 3,
        userId: 'xxxx',
        address: '测试地址3'
      }
    ]
  }
]
文章知识点

原文地址:https://blog.csdn.net/lbh_paopao/article/details/130523677

标签:语句,name,MongoDB,age,28,db,userInfo,find,select
From: https://www.cnblogs.com/nsw2018/p/17490496.html

相关文章

  • js循环语句
    js循环语句原文件代码如下:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>循环</title></head><body><Ul><li>001</li><li>002</li><li>003&......
  • MySQL8(glibc2.28)二进制安装
    操作系统:centos7.9安装数据库:mysql-8.0.33-linux-glibc2.28一、升级gcc(传送阵:升级gcc)1、添加mysql8依赖库软连接mv/lib64/libstdc++.so.6/lib64/libstdc++.so.6_oldln-s/usr/local/gcc/lib64/libstdc++.so.6/lib64/libstdc++.so.62、查看GLIBC_2.28是否更新成功stri......
  • [ABC282Ex] Min + Sum
    [ABC282Ex]Min+Sum一道分治题。比较新的地方在于,别的题都是按中点为M分治,而这道题是按最小值为M分治。记录b的前缀和sum。【L,R】最小值为M,则分为【L,M-1】,【M+1,R】。 #include<bits/stdc++.h>usingnamespacestd;constintN=4e5+66;typedeflonglongll;constll......
  • Switch语句的反汇编以及在IDA中的识别
    Switch语句的反汇编以及在IDA中的识别Switch分为四种情况,这里只介绍三种。分支较少时,不生成大表,也不生成小表,会生成if...else语句分支达到一定数量时,生成大表,且大表跟顺序无关大表可以理解为一个存储了多个地址的连续表,通过Register*4可以来寻址。分支达到一定数量,生成大......
  • SQL语句_链表(上)
    说到连表查询,我们先了解下别名。别名可以用在表上,也可以用在表中参数名。即 SELECT"表格别名"."表中参数名""表中参数别名"FROM“表格名” "表格别名" 或 SELECT"表格别名"."表中参数名"AS"表中参数别名"FROM“表格名”AS"表格别名" 举个例子 SELECTSI.st......
  • MongoDB4.0.0 远程连接及用户名密码认证登陆配置
    https://blog.csdn.net/qq_26896281/article/details/81206492参考文章:https://docs.mongodb.com/manual/tutorial/enable-authentication/https://docs.mongodb.com/manual/core/security-built-in-roles/https://blog.csdn.net/Keith003/article/details/80897085一、创建......
  • 在执行建表语句时提示:Row size too large (> 8126)
    在执行建表语句时提示:在mysqld节点下添加以下配置,然后重启mysql[mysqld]innodb_log_file_size=500Minnodb_log_buffer_size=800Minnodb_strict_mode=0 ......
  • nrf52832学习-app_timer模块
    简单使用:app_timer是模块,使用时需要在sdk_config.h文件中将其使能添加头文件app_timer.h定义Timer定时器唯一识别号_my_timer_id和需要的定时时间常量MY_TIMER_INTERVALAPP_TIMER_DEF(_my_timer_id);#defineMY_TIMER_INTERVALAPP_TIMER_TICKS(_milliseconds)......
  • nrf52832学习-app_scheduler模块
    软件调度模块,但不是用来做任务调度的,而是为了在main函数中执行中断任务。说白了就是一个队列,中断中将数据和执行的函数放入队列中,然后main函数里一直去调用查询去执行,从而不阻塞中断app_sched_execute用来在main函数中执行,一直调用app_sched_event_put函数是在中断中调用将任务添......
  • java 如何实现控制台打印sql语句
    springboot项目打印sql语句2019.9.27首先我们看一下效果现在直接上代码:logback-spring.xml文件<configuration><!--%m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,,--><!--appender是configuration的子节点,是负责......