2

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.

1 Answer 1

2

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 }} ]) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.