MongoDB $group (mongo playground)

[亡魂溺海] 提交于 2021-01-28 19:36:36

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/63908926/mongodb-group-mongo-playground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!