MongoDB $ group(Mongo游乐场) [英] MongoDB $group (mongo playground)

查看:79
本文介绍了MongoDB $ group(Mongo游乐场)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处询问了有关$ unwind运算符的问题,但是在将数据正确分组之后面临着问题.

I have asked a question here about the $unwind operator, but am facing issues with grouping the data properly after.

我有一个 mongo游乐场,但这里也是.在查询中输入 $ unwind $ lookup $ group 之后(也许有更好,更有效的方法吗?),我剩下的数据是:

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"
  }
]

我想做的是,根据等效的ExerciseId将每个Exercise Detail对象添加到各自的Sets对象中,以便最终结果如下:

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"
        }
     ]
}

任何人都可以帮助进行正确的分组吗?(如果您看到一种更好的$ unwind方法,还可以$ lookup吗?)

Can anyone help with the proper grouping? (and if you see a better way to $unwind, $lookup also?)

推荐答案

您需要另外两个阶段.首先,您可以运行 $ reduce 来压平 ExerciseDetail 现在是一个数组数组.完成后,您可以运行 $ map 嵌套 $ filter Sets配对 ExerciseDetails :

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"
                            ]
                        }
                    }
                }
            }
        }
    }
}

蒙戈游乐场

这篇关于MongoDB $ group(Mongo游乐场)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆