将毫秒转换为 mongodb 分组依据聚合管道中的日期?

convert milliseconds to date in mongodb aggregation pipeline for group by?

我必须在 mongodb 聚合管道中将毫秒转换为日期格式 -

我的查询是 -

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

在此查询中如何将 txnTime(以毫秒为单位)转换为管道中的日期?

我试图了解将 txnTime 字段转换为日期对象背后的逻辑,因为按日期字段或以毫秒为单位的时间戳分组(就像您目前正在做的那样)将产生相同的结果结果是因为它们在各自的格式上都是独一无二的!

要将 txnTime 字段更改为日期对象,您应该使用此表达式

包含一个 $project pipeline before the $group 管道阶段
"txnTime": {
    "$add": [ new Date(0), "$txnTime" ]
}

以便您可以在 converted/projected txnTime 字段上执行 $group 操作:

var convertedTxnTime = { "$add": [new Date(0), "$txnTime"] };

/*
  If using MongoDB 4.0 and newer, use $toDate 

  var convertedTxnTime = { "$toDate": "$txnTime" };

  or $convert

  var convertedTxnTime = { "$convert": { "input": "$txnTime", "to": "date" } };

*/

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

输出:(基于中的示例文档)

/* 0 */
{
    "result" : [ 
        {
            "_id" : {
                "txnTime" : ISODate("2015-04-25T18:30:00.000Z"),
                "msisdn" : "91808770101"
            },
            "msisdnCount" : 1
        }, 
        {
            "_id" : {
                "txnTime" : ISODate("2015-04-27T05:11:54.796Z"),
                "msisdn" : "9180877010"
            },
            "msisdnCount" : 1
        }, 
        {
            "_id" : {
                "txnTime" : ISODate("2015-04-25T18:30:01.111Z"),
                "msisdn" : "91808070101"
            },
            "msisdnCount" : 1
        }, 
        {
            "_id" : {
                "txnTime" : ISODate("2015-04-25T18:30:00.000Z"),
                "msisdn" : "91808070101"
            },
            "msisdnCount" : 2
        }, 
        {
            "_id" : {
                "txnTime" : ISODate("2015-04-27T05:11:54.796Z"),
                "msisdn" : "9189877000"
            },
            "msisdnCount" : 1
        }, 
        {
            "_id" : {
                "txnTime" : ISODate("2015-04-27T05:11:54.796Z"),
                "msisdn" : "9189877667"
            },
            "msisdnCount" : 1
        }
    ],
    "ok" : 1
}

-- 更新 --

要按 YYYY-MM-DD 格式的日期对文档进行分组,请使用 Date Aggregation Operators

示例:

var convertedTxnTime = { "$add": [new Date(0), "$txnTime"] };

/*
  If using MongoDB 4.0 and newer, use $toDate 

  var convertedTxnTime = { "$toDate": "$txnTime" };

  or $convert

  var convertedTxnTime = { "$convert": { "input": "$txnTime", "to": "date" } };

*/

db.campaign_wallet.aggregate([
    { "$match": { 
        "campaignId" : 1 , 
        "txnTime" : { 
            "$gte" : 1429554600000 , 
            "$lte" : 1430159400000
        }
    } },
    { "$group" : { 
        "_id" : {
            "txnTime_year" : { "$year": convertedTxnTime },
            "txnTime_month" : { "$month": convertedTxnTime },
            "txnTime_day" : { "$dayOfMonth": convertedTxnTime },
            "msisdn": "$msisdn"
        }, 
        "msisdnCount" : { "$sum" : 1}
    } }
]);

输出:

/* 0 */
{
    "result" : [ 
        {
            "_id" : {
                "txnTime_year" : 2015,
                "txnTime_month" : 4,
                "txnTime_day" : 25,
                "msisdn" : "91808770101"
            },
            "msisdnCount" : 1
        }, 
        {
            "_id" : {
                "txnTime_year" : 2015,
                "txnTime_month" : 4,
                "txnTime_day" : 25,
                "msisdn" : "91808070101"
            },
            "msisdnCount" : 3
        }, 
        {
            "_id" : {
                "txnTime_year" : 2015,
                "txnTime_month" : 4,
                "txnTime_day" : 27,
                "msisdn" : "9180877010"
            },
            "msisdnCount" : 1
        }, 
        {
            "_id" : {
                "txnTime_year" : 2015,
                "txnTime_month" : 4,
                "txnTime_day" : 27,
                "msisdn" : "9189877000"
            },
            "msisdnCount" : 1
        }, 
        {
            "_id" : {
                "txnTime_year" : 2015,
                "txnTime_month" : 4,
                "txnTime_day" : 27,
                "msisdn" : "9189877667"
            },
            "msisdnCount" : 1
        }
    ],
    "ok" : 1
}

使用 mongodb 4.0,您可以尝试 $toDate 聚合将毫秒转换为日期格式

db.collection.aggregate([
  { "$match": { 
    "campaignId" : 1 , 
    "txnTime" : { 
      "$gte" : 1429554600000 , 
      "$lte" : 1430159400000
    }
  }},
  { "$project": {
    "toDate": {
      "$toDate": "$txnTime"
    }
  }}
])

你可以试试here