如何在 mongodb 聚合中将毫秒转换为日期?

How to convert milliseconds to date in mongodb aggregation?

我在 MongoDB 中有一个 collection 和这样的文档 -

[
    {
        "campaignId": 1,
        "operatorId": 1,
        "txnType": "DR",
        "amount": 2,
        "balance": 0,
        "txnTime": 1428907779206,
        "txnSrc": "Dial_In",
        "msisdn": "9789877667",
        "circle": "Delhi",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 1,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Delhi",
        "amount": 2,
        "balance": 0,
        "txnTime": 1430111514796,
        "txnSrc": "Dial_In",
        "msisdn": "9189877667",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 1,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Delhi",
        "amount": 2,
        "balance": 0,
        "txnTime": 1430111514796,
        "txnSrc": "Dial_In",
        "msisdn": "9189877000",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 8,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Delhi",
        "amount": 2,
        "balance": 0,
        "txnTime": 1430111514796,
        "txnSrc": "Dial_In",
        "msisdn": "9189877010",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 8,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Mumbai",
        "amount": 2,
        "balance": 0,
        "txnTime": 1430111514796,
        "txnSrc": "Dial_In",
        "msisdn": "9180877010",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 1,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Mumbai",
        "amount": 2,
        "balance": 0,
        "txnTime": 1430111514796,
        "txnSrc": "Dial_In",
        "msisdn": "9180877010",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 1,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Mumbai",
        "amount": 2,
        "balance": 0,
        "txnTime": 1429986600000,
        "txnSrc": "Dial_In",
        "msisdn": "91808770101",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 1,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Delhi",
        "amount": 2,
        "balance": 0,
        "txnTime": 1429986600000,
        "txnSrc": "Dial_In",
        "msisdn": "91808070101",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 1,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Delhi",
        "amount": 2,
        "balance": 0,
        "txnTime": 1429986600000,
        "txnSrc": "Dial_In",
        "msisdn": "91808070101",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 8,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Jaipur",
        "amount": 2,
        "balance": 0,
        "txnTime": 1430111514796,
        "txnSrc": "Dial_In",
        "msisdn": "9180877010",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 8,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "UP-West",
        "amount": 2,
        "balance": 0,
        "txnTime": 1430111514796,
        "txnSrc": "Dial_In",
        "msisdn": "9180877010",
        "smsContent": "Hello Mr Arif"
    },
    {
        "campaignId": 1,
        "operatorId": 1,
        "txnType": "DR",
        "circle": "Delhi",
        "amount": 2,
        "balance": 0,
        "txnTime": 1429986601111,
        "txnSrc": "Dial_In",
        "msisdn": "91808070101",
        "smsContent": "Hello Mr Arif"
    }
]

我根据日期的唯一 msisdn 为这个 collection 进行了分组依据聚合查询,即 -

     db.campaign_wallet.aggregate({
   "$match": {
     "campaignId": 1,
     "txnTime": {
       "$gte": 1429554600000,
       "$lte": 1430159400000
     }
   }
 }, {
   "$group": {
     "_id": {
       "txnTime": "$txnTime",
       "msisdn": "$msisdn"
     },
     "msisdnCount": {
       "$sum": 1
     }
   }
 }, {
   "$group": {
     "_id": "$_id.txnTime",
     "msisdns": {
       "$push": {
         "txnTime": "$_id.txnTime",
         "count": "$msisdnCount"
       },
     },
     "count": {
       "$sum": "$msisdnCount"
     }
   }
 });

这是根据以毫秒为单位的时间和 msisdns 给出的正确结果 -

我必须在我的查询中将时间(毫秒)转换为日期,以便它将根据日期而不是精确时间(以毫秒为单位)过滤数据。解决方法是什么?

您可以尝试将毫秒时间添加到 $project operator using the $add 算术运算符中的零毫秒 Date() 对象,因此如下所示的聚合管道将为您提供转换为日期的时间戳字段:

db.campaign_wallet.aggregate([
    { 
        "$match": { 
            "campaignId" : 1 , 
            "txnTime" : { 
                "$gte" : 1429554600000 , 
                "$lte" : 1430159400000
            }
        }
    },
    { 
        "$group" : { 
            "_id" : {
                "txnTime" : "$txnTime",
                "msisdn":"$msisdn"
            }, 
            "msisdnCount" : { "$sum" : 1}
        }
    },
    { 
        "$group" : { 
            "_id" : "$_id.txnTime", 
            "msisdns" : { 
                "$push" :{
                    "txnTime" : "$_id.txnTime", 
                    "count" : "$msisdnCount"
                },
            }, 
            "count" : { 
                "$sum" : "$msisdnCount"
            }
        }
    },
    {
        "$unwind": "$msisdns"
    },
    {
        "$project": {
            "msisdns": {
                "txnTime" : {
                    "$add": [ new Date(0), "$msisdns.txnTime" ]
                }
            },
            "msisdns.count": 1,
            "count": 1
         } 
    }
]);

输出:

/* 0 */
{
    "result" : [ 
        {
            "_id" : 1430111514796,
            "msisdns" : {
                "txnTime" : ISODate("2015-04-27T05:11:54.796Z"),
                "count" : 1
            },
            "count" : 1
        }, 
        {
            "_id" : 1430111514900,
            "msisdns" : {
                "txnTime" : ISODate("2015-04-27T05:11:54.900Z"),
                "count" : 1
            },
            "count" : 1
        }
    ],
    "ok" : 1
}