Ridge Robinson
Ridge Robinson

Reputation: 758

MongoDB $group (mongo playground)

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?)

Upvotes: 3

Views: 947

Answers (1)

mickl
mickl

Reputation: 49945

You need two additional stages. Firstly you can run $reduce to flatten ExerciseDetail which now is an array of arrays. Once it's done you can run $map with nested $filter to pair Sets with ExerciseDetails:

{
    $addFields: {
        ExerciseDetail: {
            $reduce: {
                input: "$ExerciseDetail",
                initialValue: [],
                in: {
                    $concatArrays: [ "$$value", "$$this" ]
                }
            }
        }
    }
},

{
    $project: {
        _id: 1,
        Sets: {
            $map: {
                input: "$Sets",
                as: "set",
                in: {
                    $let: {
                        vars: {
                            exDetail: {
                                $arrayElemAt: [
                                    { $filter: { input: "$ExerciseDetail", cond: { $eq: [ "$$this._id", "$$set.ExerciseId" ] } } },
                                    0
                                ]
                            }
                        },
                        in: {
                            $mergeObjects: [
                                "$$set", "$$exDetail"
                            ]
                        }
                    }
                }
            }
        }
    }
}

Mongo Playground

Upvotes: 2

Related Questions