猫鼬聚合与 $lookup 中的对象数组

时间:2021-04-29 19:26:37

标签: node.js arrays mongodb mongoose aggregation-framework

我有以下收藏:


Collection A:

{
    _id: 123,
    name: "A",
    amount: 2,
    some_other_information: "ttt"
}
{
    _id: 223,
    name: "B",
    amount: 2,
    some_other_information: "ggg"
}

Collection B: 

{
    _id: 123,
    name: "K",
    amount: 2,
    some_other_information: "fff"
}
{
    _id: 2,
    name: "L",
    amount: 2,
    some_other_information: "vvv"
}

Collection: D

{
    _id: 123,
    name: "test",
    items: [
        {
            _id: 1,
            a_id: 123,
            b_id: 1,
            c_id: 123
        },
        {
            _id: 2,
            a_id: 223,
            b_id: 2,
            c_id: 223
        },
        {
            _id: 3,
            a_id: 345,
            b_id: 3
        },
    ]
}

我想将集合 D 与集合 A 聚合,并使用 a_id 数组中 D 集合中的 items

所以我有以下输出:

{
    _id: 123,
    name: "test",
    items: [
        {
            _id: 1,
            a: {
                _id: 123,
                name: "A",
                amount: 2,
                some_other_information: "ttt"
            },
            b: {
                _id: 123,
                name: "K",
                amount: 2,
                some_other_information: "fff"
            },
            c: {
                _id: 123,
                name: "A",
                amount: 2,
                some_other_information: "ttt"
            }
        },
        {
            _id: 2,
            a: {
                _id: 223,
                name: "B",
                amount: 2,
                some_other_information: "ggg"
            },
            b: {
                _id: 2,
                name: "L",
                amount: 2,
                some_other_information: "vvv"
            },
            c: {
                _id: 223,
                name: "B",
                amount: 2,
                some_other_information: "ggg"
            }
        }
    ]
}

我试过了

    const x = await D.aggregate([
        {
          $lookup: {
            from: "A",
            localField: "items.a_id",
            foreignField: "_id",
            as: "a_items",
          },
        },
        {
            $project: {
                _id: 1,
                name: 1,
                items: {
                $map: {
                    input: "$a_items",
                    as: "ri",
                    in: {
                    $mergeObjects: [
                        "$$ri",
                        {
                        $arrayElemAt: [
                            {
                            $filter: {
                                input: "$items",
                                cond: {
                                $eq: [
                                    "$$this._id",
                                    "$$ri.a_id"
                                ]
                                }
                             }
                            },
                            0
                         ]
                        }
                    ],
                    }
                  }
                }
            }
        }]);

但通过这种方式,它不会仅与 a_id 聚合。它显示了 A Collection 中的每个项目。更不用说它不包含c_id(与a_id相同,但不是必需的,因此可以为null)。

所以我不知道该尝试什么了。如果有人可以提供帮助,那将非常有帮助。

提前致谢!

1 个答案:

答案 0 :(得分:0)

  • $lookup 与集合 A
  • $lookup 与集合 B
  • 如果你想要集合 C,你可以添加更多查找
  • $map 迭代 items 数组的循环
  • in 中显示必填字段
  • a 添加一个字段并从 items 中过滤 a_items 并获取匹配元素并获取第一个元素
  • b 添加一个字段并从 items 中过滤 b_items 并获取匹配元素并获取第一个元素
  • 与您可以根据需要添加 c 相同
db.colD.aggregate([
  {
    $lookup: {
      from: "colA",
      localField: "items.a_id",
      foreignField: "_id",
      as: "a_items"
    }
  },
  {
    $lookup: {
      from: "colB",
      localField: "items.b_id",
      foreignField: "_id",
      as: "b_items"
    }
  },
  {
    $project: {
      _id: 1,
      name: 1,
      items: {
        $map: {
          input: "$items",
          as: "i",
          in: {
            _id: "$$i._id",
            a: {
              $arrayElemAt: [
                {
                  $filter: {
                    input: "$a_items",
                    cond: { $eq: ["$$this._id", "$$i.a_id"] }
                  }
                },
                0
              ]
            },
            b: {
              $arrayElemAt: [
                {
                  $filter: {
                    input: "$b_items",
                    cond: { $eq: ["$$this._id", "$$i.b_id"] }
                  }
                },
                0
              ]
            }
          }
        }
      }
    }
  }
])

Playground