mongodb_查询与聚合

一、mongodb查询语句

1.查询文档

1.where语句:查询user集合中字段 name='sitven' 的数据 (mongodb中查询条件用键值对表示)
 get.Collection('user').find({name:'sitven'})

2.and语句:查询user集合中name='sitven' and set_up = '2019-03-01' 的数据 (mongodb中and条件用','隔开)
 get.Collection('user').find({name:'sitven', set_up:'2019-03-01'})

3.or语句:查询user集合中name='勇敬' or name='海林'的数据($or定义'或' 列表中字典为查询条件)
 get.Collection('user').find({$or: [{name:'勇敬'},{name:'海林'}]})

4.and和or语句联合:查询user集合中set_up='2019-03-01' and(name='勇敬' or name='海林')的数据
 get.Collection('user').find({set_up:'2019-03-01', $or: [{name:'勇敬'},{name:'海林'}]})

2.条件操作符

操作 格式 范例 关系型数据库中类似
等于 {key: 'value'} db.user.find({"age":"50"}) where age = 50
小于 {key:{$lt: 'value'}} db.user.find({"age":{$lt:50}}) where age < 50
小于或等于 {key:{$lte: 'value'}} db.user.find({"age":{$lte:50}}) where age <= 50
大于 {key:{$gt: 'value'}} db.user.find({"age":{$gt:50}}) where age > 50
大于或等于 {key:{$gte: 'value'}} db.user.find({"age":{$gte:50}}) where age >= 50
不等于 {key:{$ne: 'value'}} db.user.find({"age":{$ne:50}}) where age != 50

3.模糊查询

1.左关键字查询: 查询user集合中字段name以‘勇’开头的文档:  get.Collection('user').find({name: /^勇/})

2.右关键字查询: 查询user集合中字段name以‘林’结尾的文档:  get.Collection('user').find({name: /林$/})

3.包含查询: 查询user集合中字段name包含"文"字的文档:      get.Collection('user').find({name: /文/})

4.聚合函数

$group集合文档分组,统计结果

求和:sum
查询user集合并根据type字段的分组对age和number字段进行求和(不分组则将_id:'$type'的值改成_id:null)
db.getCollection('user').aggregate([{$group:{_id:'$type',age:{$sum:'$age'},number:{$sum:'$number'}}])
加入查询条件对查询结果进行分组:birthday=2018-03-01
db.getCollection('user').aggregate([{$match:{birthday:2018-03-01}},{$group:{_id:'$type',age:{$sum:'$age'},number:{$sum:'$number'}}}])

平均值: avg
根据条件birthday=2018-03-01,查询user集合并获取age和number的平均值
db.getCollection('user').aggregate([{$match:{birthday:2018-03-01}},{$group:{_id:null,age:{$avg:'$age'},number:{$avg:'$number'}}}])

最大值: max
根据条件type=woman,查询user集合并获取age和number的最大值
db.getCollection('user').aggregate([{$match:{type:'woman'}}{$group:{_id:null,age:{$max:'$age'},number:{$max:'$number'}}}])

最小值: min
根据条件first_name=Lee,查询user集合并获取age和number的最大值
db.getCollection('user').aggregate([{$match:{first:'Lee'}}{$group:{_id:null,age:{$min:'$age'},number:{$min:'$number'}}}])

5.管道操作符

   $sort:将输入文档排序后输出

   $group:将集合中的文档分组,可用于统计结果

   $limit:用来限制MongoDB聚合管道返回的文档数

   $skip:在聚合管道中跳过指定数量的文档,并返回余下的文档

   $unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值

   $match:过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操作

   $project:修改输入文档结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档


6. 排序&跳过&指定数量&显示和隐藏字段&统计数据数量

排序:sort

查询user集合中type=man的数据并根据birthday进行正序排列(字段为1,正序)
db.getCollection('user').find({type:'man'}).sort({birthday:1})
查询user集合中type=man的数据并根据birthday进行倒序排列(字段为-1,倒序)
db.getCollection('user').find({type:'man'}).sort({birthday:-1})

跳过指定数量: skip
查询user集合并跳过前5条数据
db.getCollection('user').find({}).skip(5)

指定数量:limit
查询user集合并只显示前5条数据
db.getCollection('user').find({}).limit(5)

显示&隐藏字段
# 显示与隐藏不可并列使用
查询user集合只获取前5条数据,并只显示name、type和birthday(字段值为1显示)
db.getCollection('user').find({},{name:1,type:1,birthday:1}).limit(5)
查询user集合跳过前5条数据,并隐藏mobile和city(字段值为0则隐藏)
db.getCollection('user').find({},{mobile:0,city:0}).limit(5)

统计数据数量:count
获取user表中age大于等于25的总人数
db.getCollection('user').count({age:{$get:25}})

猜你喜欢

转载自blog.csdn.net/weixin_43507959/article/details/88857786