MongoDB $从一个文档中查找多个localField

时间:2018-06-18 10:10:42

标签: mongodb

我有Adresses模特:

{
    user1: ObjectId(),
    user2: ObjectId()
}

每个用户字段都是对用户模型的引用。 是否可以执行一次传播数据的查找?

1 个答案:

答案 0 :(得分:0)

您可以按如下方式放置多个参考:

collection.aggregate([
    {
        $lookup: {
           from: "whereyouwant",
           localField: "localfield",
           foreignField: "foreignfield",
           as: "name1"
        }
    },
    {
        $lookup: {
           from: "whereyouwant2",
           localField: "localfield2",
           foreignField: "foreignfield2",
           as: "name2"
        }
    },... rest of code