mongo聚合查询操作

Mongo的aggregate查询操作

如果你想做的就不仅仅是将数据从数据里面取出来,那么就需要使用聚合查询

一般聚合操作 由一连串的构件组成
主要有筛选(

关键字

    • Mongo的aggregate查询操作
      • $ match
      • $ project
      • $ group $sum $first $last $push $slice $cond
      • $ sort
      • $ limit $skip
      • $ unwind

$ match

用于对文档数据进行筛选

db.temperature.aggregate([{$match:{}}])
//效果相当于
 db.temperature.find({})

至于查询条件我就不介绍了 可以使用所有的常规操作符 举个例子

{ "_id" : ObjectId("5f1950202706fa2a11e6af75"), "project" : "5f192d51cb387a69229e28d0", "account" : "robot_qkjx8034", "robot" : "ZZPR1120B000SZZH010720200707", "face" : "5f19501edf138e4a43e5eaa9", "person" : { "type" : "stranger", "id" : "unknown", "name" : "unknown", "idcard" : "" }, "status" : "normal", "temperature" : 34.9, "description" : "1", "createtime" : 1595494429 }
{ "_id" : ObjectId("5f1950ae2706fa2a11e6af76"), "project" : "5f192d51cb387a69229e28d0", "account" : "robot_qkjx8034", "robot" : "ZZPR1120B000SZZH010720200707", "face" : "5f1950ac24518c4a444638a2", "person" : { "type" : "stranger", "id" : "unknown", "name" : "unknown", "idcard" : "" }, "status" : "normal", "temperature" : 35, "description" : "1", "createtime" : 1595494570 }
{ "_id" : ObjectId("5f1951312706fa2a11e6af77"), "project" : "5f192d51cb387a69229e28d0", "account" : "robot_qkjx8034", "robot" : "ZZPR1120B000SZZH010720200707", "face" : "5f195130d8faef4a4ab8021d", "person" : { "type" : "stranger", "id" : "unknown", "name" : "unknown", "idcard" : "" }, "status" : "normal", "temperature" : 34.8, "description" : "1", "createtime" : 1595494702 }
{ "_id" : ObjectId("5f1951392706fa2a11e6af78"), "project" : "5f192d51cb387a69229e28d0", "account" : "robot_qkjx8034", "robot" : "ZZPR1120B000SZZH010720200707", "face" : "5f1951383cecd84a60c25e7c", "person" : { "type" : "stranger", "id" : "unknown", "name" : "unknown", "idcard" : "" }, "status" : "normal", "temperature" : 34.8, "description" : "1", "createtime" : 1595494710 }
{ "_id" : ObjectId("5f19535a2706fa2a11e6af79"), "project" : "5f192d51cb387a69229e28d0", "account" : "robot_qkjx8034", "robot" : "ZZPR1120B000SZZH010720200707", "face" : "5f195357df138e4a43e5eaaa", "person" : { "type" : "stranger", "id" : "unknown", "name" : "unknown", "idcard" : "" }, "status" : "normal", "temperature" : 34.9, "description" : "1", "createtime" : 1595495254 }

我要查询 createtime 字段为 1595495254的数据

 db.temperature.find({createtime:1595495254})

在这里插入图片描述

db.temperature.aggregate([{$match:{createtime:1595495254}}])

在这里插入图片描述
效果是一样的

db.temperature.aggregate([{$addFields:{id:{$toString:'$_id'}}},{$match:{}},{$project:{_id:0}}])

将objectid变id(很实用)

$ project

用于从$match 筛选结果中提取字段

例如 只想取用结果中的 project 和 createtime

 db.temperature.aggregate([{$match:{createtime:1595495254}},{$project:{project:1,createtime:1,_id:0}}])

在这里插入图片描述
注明 字段名:0代表过滤 :1 代表 选中的字段

改字段名操作

db.temperature.aggregate([{$match:{createtime:1595495254}},{$project:{project_xxx:'$project',createtime:1,_id:0}}])

还可以使用一些其他字符串表达式,下面列举一些我平时用过的一些表达式(手动狗头)
$ add :[…]
表示将这些字段相加 注明字段要加上$

db.xxx.aggregate([{$match:{}},{$project:{totol:{$add:[$fields1,$fields]}}}])
//表示将 fields1 ,fields2 两个字段值相加 得到结果赋值给 totol 再返回 

$substract:[expr1,expr2]
表示用第一个字段减去第二个字段 作为结果

$multiply:[expr1,expr2]…
表示多个字段相乘 作为结果

以上操作前提是字段类型必须是数值

$ group $sum $first $last $push $slice $cond

单个字段分组

在这里插入图片描述
直接 _id:$字段名
多个字段分组
在这里插入图片描述

_id:{'project':'$project',createtime:'$createtime'}

以project 和 createtime 分组

分组后想加入字段 操作如下
在这里插入图片描述

db.temperature.aggregate([{$match:{createtime:1595495254}},{$group:{_id:{'project':'$project',createtime:'$createtime'},account:{$first:'$account'}}}])

$first 表示每个分组第一个数据的信息,可以取用里面的值
上面查询语句的意思就是 每组信息返回第一个数据的account字段

$last 表示最后一个数据

{ "_id" : "KWPR1120A000SZZH011119200043", "account" : "zhaccd1026", "area" : "", "begin" : 1583078400, "description" : "", "end" : 1901203200, "group" : "", "mfrs" : "ciot", "model" : "PR1120A", "name" : "智慧广场2F-43", "projectid" : "5e0b17c8fd79d3137850ee3a", "type" : "PR", "version" : "V2.0.18_beta", "x" : 0, "y" : 0, "alias" : "", "user" : "robot_zhaccd1026", "status" : "activate" }
{ "_id" : "KWPR1120A000SZZH011119200044", "account" : "szkszn9904", "area" : "5e62040bfac083309479986c", "begin" : 1583482631, "description" : "", "end" : 1930406400, "group" : "", "mfrs" : "ciot", "model" : "PR1120A", "name" : "深圳科松智 能-1.4-44号机", "projectid" : "5e62040b42066924c4e362bc", "type" : "PR", "version" : "V2.0.18_beta", "x" : 0, "y" : 0, "alias" : "", "user" : "robot_szkszn9904", "status" : "activate" }
{ "_id" : "KWPR1120A000SZZH280419200001", "account" : "lhkss3363", "alias" : "", "area" : "", "begin" : 1583337600, "description" : "", "end" : 1901203200, "group" : "", "mfrs" : "ciot", "model" : "PR1120A", "name" : "罗湖看守所", "projectid" : "5e5e4a9b42066924c4e362a2", "type" : "PR", "version" : "V2.0.18_beta", "x" : 0, "y" : 0, "user" : "robot_lhkss3363", "status" : "activate" }
{ "_id" : "KWPR1120A000SZZH280419200002", "account" : "shnx6035", "alias" : "", "area" : "", "begin" : 1589246218, "description" : "", "end" : 1903795200, "group" : "", "mfrs" : "ciot", "model" : "PR1120A", "name" : "上海南晓-002号机", "projectid" : "5e623f0a42066924c4e362c0", "type" : "gate", "version" : "2005072", "x" : 0, "y" : 0, "user" : "robot_shnx6035", "status" : "activate" }
{ "_id" : "KWPR1120A000SZZH280419200003", "account" : "yfcs6761", "alias" : "", "area" : "", "begin" : 1589539509, "description" : "", "end" : 1779984000, "group" : "", "mfrs" : "ciot", "model" : "PR1120A", "name" : "new", "projectid" : "5e5ce3d942066924c4e3629a", "type" : "gate", "version" : "V2.0.18_2003311_beta", "x" : 0, "y" : 0, "user" : "robot_yfcs6761", "status" : "activate" }
{ "_id" : "KWPR1120A000SZZH280419200007", "account" : "zhaccd1026", "alias" : "", "area" : "5e0b17c8cdfc6a13a4a7f7a5", "begin" : 1577955.902, "description" : "", "end" : 2182659555, "group" : "", "mfrs" : "ciot", "model" : "PR1120A", "name" : "智能访客机器人007-公司7楼", "projectid" : "5e0b17c8fd79d3137850ee3a", "type" : "gate", "version" : "V2.0.3_beta", "x" : 0, "y" : 0, "user" : "robot_zhaccd1026", "status" : "activate" }
db.robot.aggregate([{$group:{_id:'$account',arrs:{$push:'$_id'}}}])
{ "_id" : "azyhyy8017", "arrs" : [ "ZZPR1120BOOOSZZH010420200220" ] }
{ "_id" : "jbfp1351", "arrs" : [ "ZZPR1120B000SZZH010720200704" ] }
{ "_id" : "kppu0583", "arrs" : [ "ZZPR1120B000SZZH010720200699", "ZZPR1120B000SZZH010720200700" ] }
{ "_id" : "apwc1927", "arrs" : [ "ZZPR1120B000SZZH010720200686" ] }
{ "_id" : "vygw3264", "arrs" : [ "ZZPR1120B000SZZH010720200672", "ZZPR1120B000SZZH010720200673" ] }
{ "_id" : "hrwj6457", "arrs" : [ "ZZPR1120B000SZZH010620200669" ] }
{ "_id" : "buec6187", "arrs" : [ "ZZPR1120B000SZZH010620200662" ] }
{ "_id" : "ftobafwyxgs5049", "arrs" : [ "ZZPR1120B000SZZH010620200616" ] }
{ "_id" : "lyshdl8986", "arrs" : [ "ZZPR1120B000SZZH010620200610" ] }
{ "_id" : "sm73hysshkj0345", "arrs" : [ "ZZPR1120B000SZZH010620200607" ] }
{ "_id" : "adgydxwsxomzb3129", "arrs" : [ "ZZPR1120B000SZZH010620200606" ] }
{ "_id" : "adsdermyy4889", "arrs" : [ "ZZPR1120B000SZZH010620200604", "ZZPR1120B000SZZH010620200605" ] }
{ "_id" : "yuxk6186", "arrs" : [ "ZZPR1120B000SZZH010620200601" ] }
{ "_id" : "gjqxzx2998", "arrs" : [ "ZZPR1120B000SZZH010620200598" ] }
{ "_id" : "xyyxtzfx7564", "arrs" : [ "ZZPR1120B000SZZH010620200597" ] }
{ "_id" : "jzjqr4976", "arrs" : [ "ZZPR1120B000SZZH010620200591" ] }
{ "_id" : "uuof9002", "arrs" : [ "ZZPR1120B000SZZH010720200691", "ZZPR1120B000SZZH010720200693" ] }
{ "_id" : "nqhm6353", "arrs" : [ "ZZPR1120B000SZZH010620200590" ] }
{ "_id" : "lystytdfylbm9564", "arrs" : [ "ZZPR1120B000SZZH010620200583" ] }
{ "_id" : "szgzjtkjyxgs1110", "arrs" : [ "ZZPR1120B000SZZH010620200579" ] }

$push : expr
表示 不管expr 是什么值 都将它加入到数组中

上面查询表示 将每组中的主键id 都存到arrs 再返回

$slice 对数据中的数组做切割处理

db.render_queue.aggregate([
     {
        $match: {
          template_user_id: { $in: [85,86,84] },
          type:'render'
        }
      },
   { $group: { _id: '$type', data: { $push:'$$CURRENT' } } },//$$CURRENT 表示将数据全部字段
   {$project:{
      print:{
         $slice:['$data',1,2] // 表示skip 1 后取2条 ['$data',2] 只传一个参数时 表示 skip 0 取两条
      }
   }}
 ])

mongo聚合查询操作_第1张图片
$cond 判断用的 ,可以跟if then else 语句 也是配合 project 使用
mongo聚合查询操作_第2张图片

计算每组的数据的总量值
$sum 操作符
在这里插入图片描述

db.temperature.aggregate([{$match:{createtime:1595495254}},{$group:{_id:{'project':'$project',createtime:'$createtime'},sum:{$sum:1}}}])

$sum: value
value 为 1代表统计数据量 value 为某个数值字段时 表示每组中该字段的总和

$ sort

排序

{$sort:{createtime:1}}

1 为升序 -1 降序

$ limit $skip

接受值都为数值

 db.robot.aggregate([{$group:{_id:'$account',arrs:{$push:'$_id'}}},{$skip:1},{$limit:1}])
{ "_id" : "jbfp1351", "arrs" : [ "ZZPR1120B000SZZH010720200704" ] }

总结聚合查询一般按 match project sort group skip limit 顺序 操作

$ unwind

unwind一般用于数组查询
例:对于数组中的字段要进行模糊查询

{ "_id" : ObjectId("5fa92299c0d5068284408769"), "datas" : [ "你好", "好呀" ], "answer" : "good" }
{ "_id" : ObjectId("5fa922b3c0d506828440876a"), "datas" : [ "好么", "才怪" ], "answer" : "good" }
{ "_id" : ObjectId("5fa934f662dbd3acc310ae70"), "datas" : [ "不好" ], "answer" : "bad" }

第一步先使用unwind将数据分组
mongo聚合查询操作_第3张图片
第二步使用match 进行条件过滤

mongo聚合查询操作_第4张图片
第三步使用聚合将结果总结
在这里插入图片描述

(如果有不懂 或者mongo 聚合查询语句写不出来的 可以私信喔,我看到了就会回复的)
最后 希望能给关注 鼓励一下我这个crud 男孩 哈哈

最后来个黑小虎镇楼
mongo聚合查询操作_第5张图片

你可能感兴趣的:(mongodb)