【问题标题】:MongoDB $group (mongo playground)MongoDB $group(mongo游乐场)
【发布时间】:2021-01-02 15:05:30
【问题描述】:

我已经向here 提出了一个关于 $unwind 运算符的问题,但我遇到了在之后正确分组数据的问题。

我有一个mongo playground 的例子,但这里也是。在查询中的$unwind$lookup$group 之后(也许有更好、更有效的方法?),我留下了这些数据:

[
  {
    "ExerciseDetail": [
      [{ "Name": "Squat", "_id": "5f60c3b7f93d8e00a1cdf414" }],
      [{ "Name": "Deadlift", "_id": "5f60c3b7f93d8e00a1cdf415" }]
    ],
    "Sets": [
      {
        "ExerciseId": "5f60c3b7f93d8e00a1cdf414",
        "Sets": [],
        "WorkoutExerciseId": "5f60dc1069c27c015ede4e3e"
      },
      {
        "ExerciseId": "5f60c3b7f93d8e00a1cdf415",
        "Sets": [],
        "WorkoutExerciseId": "5f60dc1069c27c015ede4e34"
      }
    ],
    "_id": "5f60dc1069c27c015ede4e3e"
  }
]

但我想做的是,根据等效的 ExerciseId,将每个练习详细信息对象添加到相应的 Sets 对象中,以便最终结果如下所示:

{
     "_id": "5f60dc1069c27c015ede4e3e",
     "Sets": [
        {
           "ExerciseId": "5f60c3b7f93d8e00a1cdf414",
           "Name": "Squat",
           "Sets": [],
           "WorkoutExerciseId": "5f60dc1069c27c015ede4e3e"
        },
        {
           "ExerciseId": "5f60c3b7f93d8e00a1cdf415",
           "Name": "Deadlift",
           "Sets": [],
           "WorkoutExerciseId": "5f60dc1069c27c015ede4e34"
        }
     ]
}

任何人都可以帮助正确分组吗? (如果你发现 $unwind 的更好方法,$lookup 也是?)

【问题讨论】:

    标签: mongodb mongoose aggregation-framework


    【解决方案1】:

    您需要两个额外的阶段。首先,您可以运行$reduce 以展平ExerciseDetail,它现在是一个数组数组。完成后,您可以运行 $map 和嵌套的 $filter 以将 SetsExerciseDetails 配对:

    {
        $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

    【讨论】:

    • 这看起来很完美......我来自 SQL 世界,刚刚学习 Mongo。对于等同于 sql 中相当简单的 JOIN 而言,这似乎需要做很多工作。不管 sql 与 nosql 参数如何,这都是非常有用的示例。谢谢!
    • @RidgeRobinson 是的,没有像 join 这样的运算符用于数组,我希望我们有一个
    猜你喜欢
    • 1970-01-01
    • 2015-12-20
    • 2013-04-29
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    相关资源
    最近更新 更多