MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

Wie konvertiere ich Millisekunden bis heute in der Mongodb-Aggregation?

Sie könnten versuchen, die Zeit in Millisekunden zu einem Date()-Objekt mit null Millisekunden im $project Operator mit $add arithmetischer Operator, sodass eine Aggregationspipeline wie die folgende Ihnen das in Date:

konvertierte Timestamp-Feld liefert
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
         } 
    }
]);

Ausgabe :

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