如何在Mongo聚合结果中添加新字段?

时间:2017-01-11 14:05:08

标签: mongodb mongodb-query aggregation-framework

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

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

汇总结果:

 [
       {
         "_id": "12-1-2017",
         "delivered": 0,
         "clicked": 1,
         "unsubscribed": 1
       },
       {
         "_id": "11-1-2017",
         "delivered": 2,
         "clicked": 1,
         "unsubscribed": 0
       }
   ]
  • 是否可以在结果中添加新字段,例如" date"?

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

3 个答案:

答案 0 :(得分: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
    }
}]

答案 1 :(得分:2)

arr.map很好,但有一种更好的方法可以通过$ addFields获得相同的结果,只需在$ group stage之后插入下面的代码

{ $group :
    { _id : "$datePartDay",
        delivered : { "$sum" :'$isdelivered' }, 
        clicked : { "$sum" :'$isclicked' }, 
        unsubscribed : { "$sum" :'$isunsubscribe' } 
    }
}
{ $addFields: 
    { 
        date: new Date() 
    }
}

你会收到:

[
   {
     "_id": "12-1-2017",
     "delivered": 0,
     "clicked": 1,
     "unsubscribed": 1
     "date": 2018-02-17T06:12:53.538Z
   },
   {
     "_id": "11-1-2017",
     "delivered": 2,
     "clicked": 1,
     "unsubscribed": 0
     "date": 2018-02-17T06:12:53.538Z
   }
]

答案 2 :(得分:1)

这是可能的。

我会告诉你我这样做的方法:

CalendarView cal = (CalendarView) findViewById(R.id.calendar);
cal.setDate(new Date().getTime());