mongoDB中聚合(aggregate)的具体使用mongodb
咱们能够用$指定字段
来表示选定的document的field,另外能够使用$$ROOT
来表示选定的document的全部内容(例如:chosenDocument: {$push: '$$ROOT'}
)数组
示例:按条件查找数据,将找到的结果进行排序并分组,而且须要返回分组后的全部文档内容;$push, $addToSet实现的功能同样bash
GrowthRecordSync.aggregate([ { $match: cond }, { $sort: {time_point: 1}}, { $group: { _id: { $year: "$time_point"}, count:{ $sum: 1}, result:{ // $addToSet: "$$ROOT"} $push: "$$ROOT"} } }, { $sort: {_id: 1}} ],cb)
结果:函数
"data": [ { "_id": 2018, "count": 1, "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": "00000009", "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": 0 } ] }, { "_id": 2019, "count": 2, "result": [ { "_id": "5c6a979b1a921a16cc592fac", "createDate": "2019-02-18T11:31:39.000Z", "lastModified": "2019-02-18T11:31:39.062Z", "client": "584ec02dacbd274539b4ab16", "people": "5bf60a4dfdb0ed18594b8ce6", "people_no": "00000009", "ou_name": "33总经办", "ou": "5858a50bdc2a25120dcf5163", "time_point": "2019-02-20T00:00:00.000Z", "create_people": "585a17ae2098688d02eeade7", "type": "cus", "eveid": "5c6a979b1a921a16cc592fab", "title": "098765432dd1", "description": "88776787888", "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": 0 }, { "_id": "5c6a97271a921a16cc592fa9", "createDate": "2019-02-18T11:29:43.000Z", "lastModified": "2019-02-18T11:29:43.810Z", "client": "584ec02dacbd274539b4ab16", "people": "5bf60a4dfdb0ed18594b8ce6", "people_no": "00000009", "ou_name": "33总经办", "ou": "5858a50bdc2a25120dcf5163", "time_point": "2019-02-20T00:00:00.000Z", "create_people": "585a17ae2098688d02eeade7", "type": "cus", "eveid": "5c6a97271a921a16cc592fa8", "title": "098765432dd1", "description": "88776787888", "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": 0 } ] }
在 mongodb中用的主要是$regex来实现模糊查询
code
$project:修改输入文档的结构。能够用来重命名、增长或删除域,也能够用于建立计算结果以及嵌套文档。 $match:用于过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操做。 $limit:用来限制MongoDB聚合管道返回的文档数。 $skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。 $unwind:将文档中的某一个数组类型字段拆分红多条,每条包含数组中的一个值。
例:table1:
{_id:1,result:[ {name:"a",age:"18"}, {name:"b",age:"19"}, {name:"c",age:"20"}] }
执行db.table1.aggregate({"$unwind":"$result"})后的结果为
{id:1,result:{name:"a",age:"18"}}, {id:1,result:{name:"b",age:"19"}}, {id:1,result:{name:"c",age:"20"}}
$group:将集合中的文档分组,可用于统计结果。 $sort:将输入文档排序后输出。 $geoNear:输出接近某一地理位置的有序文档。
$setIntersection: 是取两个数组的交集blog
isShow: { $setIntersection: [ "$positions", current_positions ] }排序