如何在mongoDB中使用两个$ lookup

问题描述 投票:0回答:1

在这里,我有两个集合OrganizationsGroups,我的要求是我想检查组织表中的oldschoolID,我必须采取schoolIDname

然后我必须使用$lookup连接组织集合schoolID和组集合otherIds.schoolID,我必须制作一个名为group-section的单独数组,并推入所需的($project)值。

组织:

{
    "_id" : ObjectId("5c11efebd9cb4d35f47d6bd0"),
    "schoolID" : "123",
    "name" : "Abd"
}
arrays mongodb mongodb-query
1个回答
0
投票

在当前的第2阶段和第3阶段之间再放一个聚合阶段$ unwind。

    // Stage 2     
  {
    $lookup: {
      from: "Groups",
      localField: "orgID", // organization table org id
      foreignField: "otherIds.orgID",
      as: "group-section"
    }
  },
// New Stage
{
$unwind: {
path: "$group-section",
preserveNullAndEmptyArrays: true
}
}
  // Stage 3
  {
    $lookup: {
      from: "GroupContents",
      localField: "group-section.groupID",
      foreignField: "groupID",
      as: "group-section"
    }
  },
© www.soinside.com 2019 - 2024. All rights reserved.