I have asked a question here about the $unwind operator, but am facing issues with grouping the data properly after.
I have a mongo playground with an example, but here it is also. After an $unwind, $lookup and $group in the query (maybe there is a better, more efficient way to do it?), I am left with this data:
[
{
"ExerciseDetail": [
[{ "Name": "Squat", "_id": "5f60c3b7f93d8e00a1cdf414" }],
[{ "Name": "Deadlift", "_id": "5f60c3b7f93d8e00a1cdf415" }]
],
"Sets": [
{
"ExerciseId": "5f60c3b7f93d8e00a1cdf414",
"Sets": [],
"WorkoutExerciseId": "5f60dc1069c27c015ede4e3e"
},
{
"ExerciseId": "5f60c3b7f93d8e00a1cdf415",
"Sets": [],
"WorkoutExerciseId": "5f60dc1069c27c015ede4e34"
}
],
"_id": "5f60dc1069c27c015ede4e3e"
}
]
What I want to do though, it to have each of the Exercise Detail objects, be added to the respective Sets object, based on the equivalent ExerciseId, so that the final result would look like:
{
"_id": "5f60dc1069c27c015ede4e3e",
"Sets": [
{
"ExerciseId": "5f60c3b7f93d8e00a1cdf414",
"Name": "Squat",
"Sets": [],
"WorkoutExerciseId": "5f60dc1069c27c015ede4e3e"
},
{
"ExerciseId": "5f60c3b7f93d8e00a1cdf415",
"Name": "Deadlift",
"Sets": [],
"WorkoutExerciseId": "5f60dc1069c27c015ede4e34"
}
]
}
Can anyone help with the proper grouping? (and if you see a better way to $unwind, $lookup also?)