我需要数据数组ldl_xml_count
的总和,但我无法获得该字段的总和值。
设计数据
db.commination.findOne()
{
"_id" : ObjectId("5530dc5272b7e56b11497ce8"),
"data" : [
{
"ldl_date" : ISODate("2015-04-08T18:30:00Z")
},
{
"ldl_mmo_id" : 5
},
{
"ldl_xml_count" : 1
}
],
"masterid" : [
{
"ldl_date" : ISODate("2015-04-08T18:30:00Z")
},
{
"ldl_mmo_id" : 5
},
{
"ldl_xml_count" : 1
},
{
"ldl_master_info_id" : 11
}
],
"pubid" : [
{
"ldl_publication_info_id" : 41616,
"ldl_xml_info_id" : 37437691
}
],
"details" : [
{
"ldl_id" : 54261629,
"ldl_distribution_id" : 3289,
"ldl_local_flag" : 1,
"ldl_ftp_flag" : 0,
"ldl_time" : ISODate("2015-04-09T01:06:46Z")
}
]
}
mongo查询
db.commination.aggregate([
{
$match:{
"data.ldl_date": {$gte: new Date("2015-01-08T18:30:00Z")}
}
},
{
$unwind :"$data"
},
{
$group: {
_id:"$data.ldl_mmo_id",
total: { $sum: "$data.ldl_xml_count" }
}
}
])
输出
{ "_id" : 1, "total" : 0 }
{ "_id" : 3, "total" : 0 }
{ "_id" : 4, "total" : 0 }
{ "_id" : 5, "total" : 0 }
{ "_id" : 2, "total" : 0 }
{ "_id" : null, "total" : 500000 }
我需要明智的总和。