2017-10-08 59 views
1

我正在使用MongoDB 3.4.9,並且想要每月報告w.r.t.客戶信息,這裏有樣品例如MongoDB的記錄與收到的嵌套項目和錯誤是:無法從BSON類型字符串轉換爲聚合管道上的日期

不能從BSON類型的字符串轉換爲Date

{ 
"_id" : ObjectId("59da6a331c7a9ac0b6674fe8"), 
"date" : ISODate("2017-10-08T18:10:59.899Z"), 
"items" : [ 
    { 
     "quantity" : 1, 
     "price" : 47.11, 
     "desc" : "Item #1" 
    }, 
    { 
     "quantity" : 2, 
     "price" : 42.0, 
     "desc" : "Item #2" 
    } 
], 
"custInfo" : "Tobias Trelle, gold customer" 
    } 

{ 
"_id" : ObjectId("59da6a511c7a9ac0b6674fed"), 
"date" : ISODate("2017-10-08T18:11:28.961Z"), 
"items" : [ 
    { 
     "quantity" : 1, 
     "price" : 47.11, 
     "desc" : "Item #1" 
    }, 
    { 
     "quantity" : 2, 
     "price" : 42.0, 
     "desc" : "Item #2" 
    } 
], 
"custInfo" : "Tobias Trelle, gold customer" 
    } 

    { 
"_id" : ObjectId("59da6a511c7a9ac0b6674ff0"), 
"date" : ISODate("2017-10-08T18:11:29.133Z"), 
"items" : [ 
    { 
     "quantity" : 1, 
     "price" : 47.11, 
     "desc" : "Item #1" 
    }, 
    { 
     "quantity" : 2, 
     "price" : 42.0, 
     "desc" : "Item #2" 
    } 
], 
"custInfo" : "Tobias Trelle, gold customer" 
    } 

這裏是MongoDB的查詢計算總和custInfo month明智

db.runCommand({aggregate:"order", pipeline : 
[{$match : {$and : [{"date" : {$gte : ISODate("2016-10-08T18:10:59.899Z")}}, 
{"date" : {$lte : ISODate("2018-10-08T18:10:59.899Z")}}]}} 
, 
{ "$project" : { "custInfo" : 1 ,"count" : 1 , "date" : 1 , 
"duration" : {"$month" : [ "date"]}}}, 
{ "$group" : { "_id" : 
    { "duration" : "$duration" , "custInfo" : "$custInfo"} ,"count" : { "$sum" : 1} }} 

    ]}//, 
    //cursor:{batchSize:1000} 

) 

請幫助我哪裏錯了。

問候 克里斯

回答

1

我不知道爲什麼$month被認爲是「持續時間」在這裏,但在任何情況下,你放棄了美元符號場外變量和調用$month是有點過。這應該工作:

{ "$project" : { "custInfo" : 1 , 
      "count" : 1 , 
      "date" : 1 , 
      "duration" : {"$month" : "$date" } }} 
相關問題