如何将新字段添加到Mongo汇总结果?

Kep*_*ler 3 mongodb mongodb-query aggregation-framework

我有以下mongodb聚合查询,按日期分组(2017年1月5日至2017年1月13日)

CampaignActivity.aggregate([
        {
        "$match": {
            "$and":[{updatedAt: {$lt: new Date('1-13-2017')}},{updatedAt: {$gte: new Date('1-05-2017')}}]
        }
    },
    {
        "$project" :
        {
           _id : 0,
           "datePartDay" : {"$concat" : [
               {"$substr" : [{"$dayOfMonth" : "$updatedAt"}, 0, 2]}, "-",
               {"$substr" : [{"$month" : "$updatedAt"}, 0, 2]}, "-",
               {"$substr" : [{"$year" : "$updatedAt"}, 0, 4]}
          ] },
          "isdelivered": {"$cond": { if: { $eq: [ "$delivered", true ] }, then: 1, else: 0 }},
          "isclicked": {"$cond": { if: { $eq: [ "$clicked", true ] }, then: 1, else: 0 }},
          "isunsubscribe": {"$cond": { if: { $eq: [ "$unsubscribed", true ] }, then: 1, else: 0 }}

        }
    },

    { "$group" :
        { "_id" : "$datePartDay",
          "delivered" : { "$sum" :'$isdelivered' }, 
          "clicked" : { "$sum" :'$isclicked' }, 
          "unsubscribed" : { "$sum" :'$isunsubscribe' } 
        }
    }
    ],function (err, result) {
        if (err) {
            console.log(err);
        } else {
            console.log(result);
        }
    });
Run Code Online (Sandbox Code Playgroud)

汇总结果:

 [
       {
         "_id": "12-1-2017",
         "delivered": 0,
         "clicked": 1,
         "unsubscribed": 1
       },
       {
         "_id": "11-1-2017",
         "delivered": 2,
         "clicked": 1,
         "unsubscribed": 0
       }
   ]
Run Code Online (Sandbox Code Playgroud)
  • 是否可以添加新字段以导致集合中不存在诸如“日期”之类的内容?

  • 是否可以使用虚拟数据添加缺少的日期字段?

use*_*814 5

$project您所经历的阶段是多余的。您可以轻松地将所有逻辑转移到$group舞台上,并逐步完成$project以重新格式化您的响应。

下面是这样的。

[{
    "$match": {
        "$and": [{
            updatedAt: {
                $lt: new Date('1-13-2017')
            }
        }, {
            updatedAt: {
                $gte: new Date('1-05-2017')
            }
        }]
    }
}, {
    "$group": {
        "_id": {
            $dateToString: {
                format: "%m-%d-%Y",
                date: "$updatedAt"
            }
        },
        "delivered": {
            $sum: {
                "$cond": {
                    if: {
                        $eq: ["$delivered", true]
                    },
                    then: 1,
                    else: 0
                }
            }
        },
        "clicked": {
            $sum: {
                "$cond": {
                    if: {
                        $eq: ["$clicked", true]
                    },
                    then: 1,
                    else: 0
                }
            }
        },
        "unsubscribed": {
            $sum: {
                "$cond": {
                    if: {
                        $eq: ["$unsubscribed", true]
                    },
                    then: 1,
                    else: 0
                }
            }
        }
    }
}, {
    "$project": {
        "_id": 0,
        "date": "$_id",
        "delivered": 1,
        "clicked": 1,
        "unsubscribed": 1
    }
}]
Run Code Online (Sandbox Code Playgroud)