在mongodb聚合管道中将毫秒转换为日期为group by?

时间:2015-04-27 09:41:13

标签: mongodb mongodb-query aggregation-framework

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

我的查询是 -

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

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

2 个答案:

答案 0 :(得分:14)

我试图获得将txnTime字段转换为日期对象的逻辑,因为按日期字段或时间戳(以毫秒为单位)进行分组(就像您目前所做的那样)将产生与它们各自的格式都是独一无二的!

要将txnTime字段更改为日期对象,您应该在具有此表达式的$project管道阶段之前包含$group管道

"txnTime": {
    "$add": [ new Date(0), "$txnTime" ]
}

这样您就可以对转换/投影的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}
    } }
]);

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

/* 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
}

答案 1 :(得分:0)

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

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

您可以尝试here