I have a bunch of documents in a MongoDB collection, each of which looks like this:
{
"_id" : ObjectId("539f5556e4b032123458ba30"),
"name" : "H0031324836",
"date" : ISODate("2014-04-01T04:00:00Z"),
"dateString" : "2014-04-01",
"elements" : [
{
"start_time" : ISODate("2014-04-01T15:00:00Z"),
"end_time" : ISODate("2014-04-01T16:00:00Z"),
"duration" : NumberLong(3600000),
"value" : 0.6968
},
{
"start_time" : ISODate("2014-04-01T16:00:00Z"),
"end_time" : ISODate("2014-04-01T17:00:00Z"),
"duration" : NumberLong(3600000),
"value" : 1.4873
},
// ...
]
}
For each of these documents, I want (through the aggregation framework, ideally) to end up with a document like this:
{
"_id" : ObjectId("539f5556e4b032123458ba30"),
"name" : "H0031324836",
"date" : ISODate("2014-04-01T04:00:00Z"),
"dateString" : "2014-04-01",
"duration" : NumberLong(...blahblah...), // sum of all "$duration" fields
"value" : ...blahblah..., // sum of all "$value" fields
}
I'm not seeing a way to vectorize over the $elements array and pick out values, though - perhaps $unwind
is an option, but that seems pretty inefficient if it truly explodes to a stream of documents just so I can implode them again.
The collection is large (around half a billion docs now, will be several billion when the full data is loaded), so I'm hoping to use the aggregation framework and not MapReduce if possible.
I've got MongoDB 2.6.0, on a hash-sharded collection with 8 shards.