I have the following documents:
{
"_id" : ObjectId("540dadfcf3116b60d401c314"),
"value" : 2,
"d_c_at" : [
"2013",
"201311",
"2013w46",
"20131116"
]
}
and I want to group them by the last element in the d_c_at array (20131116) which represent the year, month and day store as destructured date.
Here is what I have so far:
db.points.aggregate(
{ $match: { "d_c_at.0": '2014' } },
{ $group: { _id: "$d_c_at.0", value: { $sum: "$value" } } }
)
which return:
{ "_id" : [ ], "value" : 1207 }
I have tried using $unwind without success:
db.points.aggregate(
{ $match: { "d_c_at.0": '2014' } },
{ $unwind: "$d_c_at" },
{ $group: { _id: "$d_c_at", value: { $sum: "$value" } } }
)
Seems almost good but it also groups on other array elements:
{ ... }
{ "_id" : "20140519", "value" : 33 }
{ "_id" : "20140707", "value" : 36 }
{ "_id" : "20140330", "value" : 37 }
{ "_id" : "20140709", "value" : -28 }
{ "_id" : "20140620", "value" : 14 }
{ "_id" : "2014w9", "value" : -250 }
{ ... }
Expected output:
{ ... }
{ "_id" : "20140519", "value" : 33 }
{ "_id" : "20140707", "value" : 36 }
{ "_id" : "20140330", "value" : 37 }
{ "_id" : "20140709", "value" : -28 }
{ "_id" : "20140620", "value" : 14 }
{ ... }
valuegroup on days.