2016-08-01 59 views
0

我有一個聚集腳本,讓我到結果集,它看起來像下面一組:MongoDB的組和不工作

{ 
"_id" : "SI-3", 
"party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
"transaction_date" : ISODate("2016-04-14T00:00:00.201+0000"), 
"transaction_net_value" : 1666.67, 
"object_category" : "Revenue", 
"object_origin_category" : "Bookkeeping", 
"object_origin" : "Sage One", 
"item_quantity" : NumberInt(10), 
"item_name" : "Product Three", 
"item_category" : "Sales Revenue", 
"item_ net_total_value" : 1666.67 
} 
{ 
"_id" : "SI-2", 
"party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
"transaction_date" : ISODate("2016-05-19T00:00:00.201+0000"), 
"transaction_net_value" : NumberInt(1500), 
"object_category" : "Revenue", 
"object_origin_category" : "Bookkeeping", 
"object_origin" : "Sage One", 
"item_quantity" : NumberInt(1), 
"item_name" : "Product One", 
"item_category" : "Sales Revenue", 
"item_ net_total_value" : NumberInt(1500) 
} 
{ 
"_id" : "SI-15", 
"party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
"transaction_date" : ISODate("2016-07-11T00:00:00.201+0000"), 
"transaction_net_value" : 8333.33, 
"object_category" : "Revenue", 
"object_origin_category" : "Bookkeeping", 
"object_origin" : "Sage One", 
"item_quantity" : NumberInt(7), 
"item_name" : "Product Four", 
"item_category" : "Sales Revenue", 
"item_ net_total_value" : 8333.33 
} 
{ 
"_id" : "SI-14", 
"party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
"transaction_date" : ISODate("2016-07-11T00:00:00.201+0000"), 
"transaction_net_value" : NumberInt(7500), 
"object_category" : "Revenue", 
"object_origin_category" : "Bookkeeping", 
"object_origin" : "Sage One", 
"item_quantity" : NumberInt(6), 
"item_name" : "Product Four", 
"item_category" : "Sales Revenue", 
"item_ net_total_value" : NumberInt(7500) 
} 
{ 
"_id" : "SI-1", 
"party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
"transaction_date" : ISODate("2016-06-16T00:00:00.201+0000"), 
"transaction_net_value" : NumberInt(1500), 
"object_category" : "Revenue", 
"object_origin_category" : "Bookkeeping", 
"object_origin" : "Sage One", 
"item_quantity" : NumberInt(1), 
"item_name" : "Product One", 
"item_category" : "Sales Revenue", 
"item_ net_total_value" : NumberInt(1500) 
} 
{ 
"_id" : "SI-1", 
"party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
"transaction_date" : ISODate("2016-06-16T00:00:00.201+0000"), 
"transaction_net_value" : NumberInt(1500), 
"object_category" : "Revenue", 
"object_origin_category" : "Bookkeeping", 
"object_origin" : "Sage One", 
"item_quantity" : NumberInt(1), 
"item_name" : "Product Twenty Two", 
"item_category" : "Sales Revenue", 
"item_ net_total_value" : NumberInt(1500) 
} 

當我加入最後階段,如下圖所示,我得到的結果不如預期的那樣,$sum。我得到0當它顯然不是0.任何想法我在我的$group階段做錯了什麼?

$group : { 
_id: {"party_uuid" : "$party_uuid", 
    "transaction_year" : {$year : "$transaction_date"}, 
    "transaction_month" : {$month : "$transaction_date"}, 
    "category" : "$item_category", 
    "object_origin_category" : "$object_origin_category", 
    "object_origin" : "$object_origin"}, 
    "month_net_value" : { $sum: "$item_net_total_value" } 
} 

這導致

{ 
"_id" : { 
    "party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
    "transaction_year" : NumberInt(2016), 
    "transaction_month" : NumberInt(6), 
    "category" : "Sales Revenue", 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"month_net_value" : NumberInt(0) 
} 
{ 
"_id" : { 
    "party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
    "transaction_year" : NumberInt(2016), 
    "transaction_month" : NumberInt(5), 
    "category" : "Sales Revenue", 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"month_net_value" : NumberInt(0) 
} 
{ 
"_id" : { 
    "party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
    "transaction_year" : NumberInt(2016), 
    "transaction_month" : NumberInt(7), 
    "category" : "Sales Revenue", 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"month_net_value" : NumberInt(0) 
} 
{ 
"_id" : { 
    "party_uuid" : "dfa1e80a-5521-11e6-beb8-9e71128cae77", 
    "transaction_year" : NumberInt(2016), 
    "transaction_month" : NumberInt(4), 
    "category" : "Sales Revenue", 
    "object_origin_category" : "Bookkeeping", 
    "object_origin" : "Sage One" 
}, 
"month_net_value" : NumberInt(0) 
} 

再次感謝

回答

1

難道說你有你的密鑰的空白:

"$item_net_total_value" 

"item_ net_total_value" 

與比較

+0

這很搞笑,絕對正確 - ffs - 謝謝你指出,doh! –

+0

好老四眼原則!別客氣 :-) – Matt