aggregate基础 使用记录

时间:2022-11-13 21:39:40

mongoDB中聚合(aggregate)的具体使用

我们可以用$指定字段来表示选定的document的field,另外可以使用$$ROOT来表示选定的document的所有内容(例如:chosenDocument: {$push: '$$ROOT'}

示例:按条件查找数据,将找到的结果进行排序并分组,并且需要返回分组后的所有文档内容;$push, $addToSet实现的功能一样

GrowthRecordSync.aggregate([
{ $match: cond },
{ $sort: {time_point: }},
{ $group: {
_id: { $year: "$time_point"},
count:{ $sum: },
result:{
// $addToSet: "$$ROOT"}
$push: "$$ROOT"}
}
},
{ $sort: {_id: }}
],cb)

结果:

"data": [
{
"_id": ,
"count": ,
"result": [
{
"_id": "5c514c3675844415dac30efc",
"createDate": "2019-01-30T07:03:18.000Z",
"lastModified": "2019-01-30T07:03:18.285Z",
"time_point": "2018-11-14T00:00:00.000Z",
"type": "entry",
"eveid": "5bf60a4dfdb0ed18594b8ce6",
"title": "A职",
"description": "",
"client": "584ec02dacbd274539b4ab16",
"people": "5bf60a4dfdb0ed18594b8ce6",
"people_no": "",
"people_name": "成成",
"ou_name": "啊总经办",
"ou": "5858a50bdc2a25120dcf5163",
"terminated": false,
"activate": true,
"block_reason": "",
"block": false,
"validTo": "9999-12-31T15:59:59.999Z",
"validFrom": "2019-01-29T16:00:00.000Z",
"__v":
}
]
},
{
"_id": ,
"count": ,
"result": [
{
"_id": "5c6a979b1a921a16cc592fac",
"createDate": "2019-02-18T11:31:39.000Z",
"lastModified": "2019-02-18T11:31:39.062Z",
"client": "584ec02dacbd274539b4ab16",
"people": "5bf60a4dfdb0ed18594b8ce6",
"people_no": "",
"ou_name": "33总经办",
"ou": "5858a50bdc2a25120dcf5163",
"time_point": "2019-02-20T00:00:00.000Z",
"create_people": "585a17ae2098688d02eeade7",
"type": "cus",
"eveid": "5c6a979b1a921a16cc592fab",
"title": "098765432dd1",
"description": "",
"terminated": false,
"activate": true,
"block_reason": "",
"block": false,
"validTo": "9999-12-31T15:59:59.999Z",
"validFrom": "2019-02-17T16:00:00.000Z",
"img": [
"5c6a97221a921a16cc592fa5"
],
"__v":
},
{
"_id": "5c6a97271a921a16cc592fa9",
"createDate": "2019-02-18T11:29:43.000Z",
"lastModified": "2019-02-18T11:29:43.810Z",
"client": "584ec02dacbd274539b4ab16",
"people": "5bf60a4dfdb0ed18594b8ce6",
"people_no": "",
"ou_name": "33总经办",
"ou": "5858a50bdc2a25120dcf5163",
"time_point": "2019-02-20T00:00:00.000Z",
"create_people": "585a17ae2098688d02eeade7",
"type": "cus",
"eveid": "5c6a97271a921a16cc592fa8",
"title": "098765432dd1",
"description": "",
"terminated": false,
"activate": true,
"block_reason": "",
"block": false,
"validTo": "9999-12-31T15:59:59.999Z",
"validFrom": "2019-02-17T16:00:00.000Z",
"img": [
"5c6a97221a921a16cc592fa5"
],
"__v":
}
]
}

选取部分字段,将"$$ROOT"换成下面想要的字段即可

$push: {"_id": "$_id", "process_code": "$process_code","process_name":"$process_name","wf_type":"$wf_type","cus_approve_event":"$cus_approve_event"}
 

mongodb-模糊查询+聚合函数+连表查询+排序

在 mongodb中用的主要是$regex来实现模糊查询

$project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
$match:用于过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操作。
$limit:用来限制MongoDB聚合管道返回的文档数。
$skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
$unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
  例:table1:
{_id:,result:[
{name:"a",age:""},
{name:"b",age:""},
{name:"c",age:""}]
}
执行db.table1.aggregate({"$unwind":"$result"})后的结果为
{id:,result:{name:"a",age:""}},
{id:,result:{name:"b",age:""}},
{id:,result:{name:"c",age:""}}
$group:将集合中的文档分组,可用于统计结果。 $sort:将输入文档排序后输出。 $geoNear:输出接近某一地理位置的有序文档。

聚合aggregate 管道运算符(全)

$setIntersection: 是取两个数组的交集

isShow: { $setIntersection: [ "$positions", current_positions ] }

实用注意事项记录:

ProcessDefine.aggregate({
$match: {process_code: 'CustomizeAttendance', block: false, client: ObjectId(client)}
},{
$lookup: { //lookup连表查询后都是数组,就算原来的结构是对象的连表后查出的结构也是数据
from: 'cusitemapproveevents', // 数据库中的表名(全部小写,默认加s)
localField: 'cus_approve_event',
foreignField: '_id',
as: 'cus_approve_event',
}
},{
$unwind:{ // 将数组结构换为对象结构
path: '$cus_approve_event',
preserveNullAndEmptyArrays:true
}
},
{
$group:{
_id: {cus_approve_event:"$cus_approve_event._id", approve_name: "$cus_approve_event.approve_name"}, //分组,一个值的分组_id:"$cus_approve_event._id", 对两个值进行分组,传对象。
       count:{ $sum: 1},
       result:{
        // $push: "$$ROOT" // 全部字段
        $push: {"_id": "$_id", "process_code": "$process_code","process_name":"$process_name","wf_type":"$wf_type","cus_approve_event":"$cus_approve_event"} } } // 只选需要的字段
  }).exec(function(err,data){
    res.json(data);
   })
}