分组并保留其余的字段
db.getCollection("facebook_ad_insight").aggregate([{
$match: {
"date_start": {
$gt: "2020-11-02",
$lt: "2020-11-16"
}
}
}, {
$group: {
_id: {
adset_id :"$account_id"
},
"doc": { "$first": "$$ROOT" }
}
},
{ "$replaceRoot": { "newRoot": "$doc" }}
])
分组汇总加排序
db.getCollection("facebook_ad_insight").aggregate([{
$match: {
"date_start": {
$gt: "2020-11-02"
}
}
}, {
$group: {
_id: {
date_start:"$date_start",
adset_id :"adset_id"
},
spend: {
$sum: { $toDouble: "$spend" }
}
},
}, {$sort:{"_id.date_start":1}}
])