Reputation: 896
I got this result coming from an Aggregation pipeline I did:
{
"_id" : ISODate("2015-05-14T08:22:09.441Z"),
"values" : {
"v1_min" : 15.872267760931187,
"v1" : 15.909139078185774,
"v1_max" : 20.6420184124931776
}
},
{
"_id" : ISODate("2015-05-13T08:22:09.441Z"),
"values" : {
"v1_min" : 2.872263320931187,
"v1" : 7.909132898185774,
"v1_max" :44.6498764124931776
}
},
{...}
do you think it's possible to get a structure like this one
{
"_id" : [ISODate("2015-05-14T08:22:09.441Z"),ISODate("2015-05-13T08:22:09.441Z")]
"values" : {
"v1_min" : [15.872267760931187, 2.872263320931187],
"v1" : [15.909139078185774, 7.909132898185774]
"v1_max" : [2.6420184124931776, 44.6498764124931776]
}
}
adding some others stages to my aggregation pipelines?
If so, how would you do?
I'd not like to handle this via code because I think MongoDB aggregation framework is faster and should do better than me.
Upvotes: 1
Views: 81
Reputation: 103375
Yes, it's quite possible indeed. The following aggregation pipeline will achieve the desired output:
db.collection.aggregate([
{
"$group": {
"_id": null,
"ids": {
"$addToSet": "$_id"
},
"v1_min": {
"$push": "$values.v1_min"
},
"v1": {
"$push": "$values.v1"
},
"v1_max": {
"$push": "$values.v1_max"
}
}
},
{
"$project": {
"_id": "$ids",
"values": {
"v1_min": "$v1_min",
"v1": "$v1",
"v1_max": "$v1_max"
}
}
}
]);
-- EDIT --
Use $push
instead of $addToSet
as the latter will only add the element if and only if the final array does not contain the element itself. (Thanks to @SylvainLeroux for the positive contributions)
Upvotes: 2