Mongodb组和总和不工作

时间:2016-08-01 22:23:59

标签: 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 个答案:

答案 0 :(得分:2)

你的密钥中是否有空格:

"item_ net_total_value"

与:比较:

"$item_net_total_value"