mongodb的一些查询

分组并保留其余的字段

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}}
])

All posts

Other pages

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注