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 ] }排序