Reputation: 483
I have some mongo documents like this:
{
"worker_id" : "x",
"minutes": [
{"min" : 10, "capacity" : 15},
{"min" : 10, "capacity" : 12},
{"min" : 12, "capacity" : 13},
{"min" : 12, "capacity" : 12}
]
}
I want to convert these documents into something like this:
{
"worker_id" : "x",
"minutes" : [
{
"min" : 10,
"capacities": [15,12]
},
{
"min" : 12,
"capacities": [13,12]
}
]
}
I am totally stuck on this, any help is appreciated.
Brugia.
Upvotes: 2
Views: 934
Reputation: 46491
You can try below aggregation
db.collection.aggregate([
{ "$unwind": "$minutes" },
{ "$group": {
"_id": { "worker_id": "$worker_id", "minutes": "$minutes.min" },
"capacities": { "$push": "$minutes.capacity" }
}},
{ "$group": {
"_id": "$_id.worker_id",
"minutes": {
"$push": {
"min": "$_id.minutes",
"capacity": "$capacities"
}
}
}},
{ "$project": { "worker_id": "$_id", "minutes": 1, "_id": 0 }}
])
Upvotes: 2