MongoDB - 如何聚合深度嵌套数组

发布于 2025-01-18 03:40:39 字数 2469 浏览 1 评论 0原文

我有以下 MongoDB 结构:

Division Collection:

{
  "_id": ObjectId("5b28cab902f28e18b863bd36"),
  "name": "Premier League",
  ...
  "teams": [
    ObjectId("5b28cab902f28e18b863bd01"),
    ObjectId("5b28cab902f28e18b863bd02"),
    ObjectId("5b28cab902f28e18b863bd03"),
    ...
  ]
  ...
},
...

Teams Collection:

{
  "_id": ObjectId("5b28cab902f28e18b863bd01"),
  "name": "Liverpool",
  ...
  "players": [
    ObjectId('5b23tmb902f28e18b863bd01'),
    ObjectId('5b23tmb902f28e18b863bd02'),
    ObjectId('5b23tmb902f28e18b863bd03'),
    ...
  ]
  ...
},
...

Players Collection:

{
  "_id": ObjectId("5b2b9a8bbda339352cc39ec1"),
  "name": "Mohamed Salah",
  "nationality": [
    ObjectId("5b23cn1902f28e18b863bd01"),
    ObjectId("5b23cn2902f28e18b863bd02"),
  ],
  ...
},
...

Countries Collection:

{
  "_id": ObjectId("5b23cn1902f28e18b863bd01"),
  "name": "England",
  ...
},
{
  "_id": ObjectId("5b23cn2902f28e18b863bd02"),
  "name": "Egypt",
  ...
},
...

如何使用 MongoDB 聚合($lookup$pipeline 等)获得如下结果:

{
  "divisions": [
    {
      "_id": ObjectId("5b28cab902f28e18b863bd36"),
      "name": "Premier League",
      ...
      "teams": [
        {
          "_id": ObjectId("5b28cab902f28e18b863bd01"),
          "name": "Liverpool",
          ...
          "players": [
            {
              "_id": ObjectId("5b23tmb902f28e18b863bd01"),
              "name": "Mohamed Salah",
              "nationality": [
                {
                  "_id": ObjectId("5b23cn2902f28e18b863bd02"),
                  "name": "Egypt",
                  ...
                },
                {
                  "_id": ObjectId("5b23cn1902f28e18b863bd01"),
                  "name": "England",
                  ...
                }
              ]
              ...
            },
            ...
          ]
        },
        ...
      ]
    },
    {
      "_id": ObjectId("5b28cab902f28e18b863bd37"),
      "name": "Championship",
      ...
    },
    ...
  ]
}

我设法进行一级合并:

db.divisions.aggregate([
  {
    $lookup: {
      from: 'teams',
      localField: 'teams',
      foreignField: '_id',
      as: 'teams'
    }
  },
])

然后我遇到了困难,所以我会很如果有人能帮助我解决这个问题,我将不胜感激。

I have the following MongoDB structure:

Division Collection:

{
  "_id": ObjectId("5b28cab902f28e18b863bd36"),
  "name": "Premier League",
  ...
  "teams": [
    ObjectId("5b28cab902f28e18b863bd01"),
    ObjectId("5b28cab902f28e18b863bd02"),
    ObjectId("5b28cab902f28e18b863bd03"),
    ...
  ]
  ...
},
...

Teams Collection:

{
  "_id": ObjectId("5b28cab902f28e18b863bd01"),
  "name": "Liverpool",
  ...
  "players": [
    ObjectId('5b23tmb902f28e18b863bd01'),
    ObjectId('5b23tmb902f28e18b863bd02'),
    ObjectId('5b23tmb902f28e18b863bd03'),
    ...
  ]
  ...
},
...

Players Collection:

{
  "_id": ObjectId("5b2b9a8bbda339352cc39ec1"),
  "name": "Mohamed Salah",
  "nationality": [
    ObjectId("5b23cn1902f28e18b863bd01"),
    ObjectId("5b23cn2902f28e18b863bd02"),
  ],
  ...
},
...

Countries Collection:

{
  "_id": ObjectId("5b23cn1902f28e18b863bd01"),
  "name": "England",
  ...
},
{
  "_id": ObjectId("5b23cn2902f28e18b863bd02"),
  "name": "Egypt",
  ...
},
...

How to get a result, which is below, using MongoDB aggregation ($lookup, $pipeline, etc):

{
  "divisions": [
    {
      "_id": ObjectId("5b28cab902f28e18b863bd36"),
      "name": "Premier League",
      ...
      "teams": [
        {
          "_id": ObjectId("5b28cab902f28e18b863bd01"),
          "name": "Liverpool",
          ...
          "players": [
            {
              "_id": ObjectId("5b23tmb902f28e18b863bd01"),
              "name": "Mohamed Salah",
              "nationality": [
                {
                  "_id": ObjectId("5b23cn2902f28e18b863bd02"),
                  "name": "Egypt",
                  ...
                },
                {
                  "_id": ObjectId("5b23cn1902f28e18b863bd01"),
                  "name": "England",
                  ...
                }
              ]
              ...
            },
            ...
          ]
        },
        ...
      ]
    },
    {
      "_id": ObjectId("5b28cab902f28e18b863bd37"),
      "name": "Championship",
      ...
    },
    ...
  ]
}

I manage to make a first-level merge:

db.divisions.aggregate([
  {
    $lookup: {
      from: 'teams',
      localField: 'teams',
      foreignField: '_id',
      as: 'teams'
    }
  },
])

and then I ran into difficulties, so I would be very grateful if someone could help me with this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘心安 2025-01-25 03:40:39

您需要使用pipeline进行多级嵌套$lookup

db.division.aggregate([
  {
    $lookup: {
      from: "teams",
      let: {
        teams: "$teams"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $in: [
                "$_id",
                "$teams"
              ]
            }
          }
        },
        {
          $lookup: {
            from: "players",
            let: {
              players: "$players"
            },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $in: [
                      "$_id",
                      "$players"
                    ]
                  }
                }
              },
              {
                $lookup: {
                  from: "countries",
                  localField: "nationality",
                  foreignField: "_id",
                  as: "nationality"
                }
              }
            ],
            as: "players"
          }
        }
      ],
      as: "teams"
    }
  }
])

示例 Mongo Playground

You need multi-level nested $lookup with pipeline.

db.division.aggregate([
  {
    $lookup: {
      from: "teams",
      let: {
        teams: "$teams"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $in: [
                "$_id",
                "$teams"
              ]
            }
          }
        },
        {
          $lookup: {
            from: "players",
            let: {
              players: "$players"
            },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $in: [
                      "$_id",
                      "$players"
                    ]
                  }
                }
              },
              {
                $lookup: {
                  from: "countries",
                  localField: "nationality",
                  foreignField: "_id",
                  as: "nationality"
                }
              }
            ],
            as: "players"
          }
        }
      ],
      as: "teams"
    }
  }
])

Sample Mongo Playground

再见回来 2025-01-25 03:40:39

也许有人会有用。还可以使用 populate 方法合并数据:

db.divisions.find(_id: division_id).populate(
  {
    path: 'teams',
    populate: {
      path: 'players',
      populate: {
        path: 'nationality'
      }
    }
  }
)

Maybe someone will be useful. Data also can be merged using the populate method:

db.divisions.find(_id: division_id).populate(
  {
    path: 'teams',
    populate: {
      path: 'players',
      populate: {
        path: 'nationality'
      }
    }
  }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文