【巨人的肩膀】MongoDB常用28条查询语句

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'
      }
    ]
  }
]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值