2015-04-27 118 views
3

我有這樣MongoDB中和文檔的集合 -如何將mongodb聚合中的毫秒數轉換爲日期?

[ 
    { 
     "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的基礎上的日期,這是 -

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

Query Result

-

這是毫秒級的MSISDN號碼給時間的基礎上,正確的結果我必須在查詢中將時間(毫秒)轉換爲日期,以便它將根據日期過濾數據,而不是精確的時間(以毫秒爲單位)。解決辦法是什麼?

+0

我想你忘了提及你實際的問題是什麼。 – Philipp

+0

@ Philipp我編輯的問題。我想在mongodb聚合查詢中更改時間毫秒。 –

回答

1

你可以嘗試使用$add算術運算符添加毫秒的時間到零毫秒日期()對象在$project操作,所以像下面將給你時間戳字段的聚合管道轉換爲日期:

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 
} 
+0

謝謝,但它沒有給出正確的結果。 –

+0

@AmitDas然後什麼應該是正確的結果? – chridam

+0

我想在一天中獨特的msisdn .....但你的查詢沒有做到這一點 –