【问题标题】:Mongoose deep populating multiple 2nd level objects not workingMongoose 深度填充多个 2 级对象不起作用
【发布时间】:2016-06-27 16:37:55
【问题描述】:

我有一个模型A 有这个字段:

var field = {
    foo: String,
    b: [{ type: Schema.Types.ObjectId, ref: 'B' }]
}  

和模型B 有这个字段:

var field = {
    c: { type: Schema.Types.ObjectId, ref: 'C' } // let's say this has 3 string field
    d: { type: Schema.Types.ObjectId, ref: 'D' } // so was this
}

基于answer by Trinh Hoang Nhu 通过这样做-

 A.find({_id:req.params.id})
    .populate({ path: 'patient', model: Patient,
        populate: {
            path: 'b',
            model: B
        },
        populate: {
            path: 'c',
            model: C
        },
    })
    .exec(function (err, doc) {
        res.send(doc);
    })

-它应该返回填充的第二级并且确实返回了,但问题是它仅在 .populate({}) 函数上声明的最后一个 path populate,在这种情况下仅填充模型 C。但是,当您使用模型B 更改它的位置时,模型B 将被填充。

上面的查询返回如下内容:

[
    {
        "foo":"Bar",
        "b": [
            {
                "c":"a32s1da4fas1a23s1da56s4c",
                "d":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                }
            },
            {
                "c":"a32s1da4fas1a23s1da56s4d",
                "d":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                }
            }
            // so on ...
        ]
    }
]

我期待这样的事情:

[
    {
        "foo":"Bar",
        "b": [
            {
                "c":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                },
                "d":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                }
            },
            {
                "c":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                },
                "d":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                }
            }
            // so on ...
        ]
    }
]

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-populate


    【解决方案1】:

    我不知道为什么它甚至接受了对象中的多个populate 键,它已经重复了, 您指定填充的是:

     populate: {
                path: 'b',
                model: B
            },
            populate: {
                path: 'c',
                model: C
            },
    

    这里属性populate 被复制,并且只考虑定义的最后一个。

    您需要将填充路径指定一次作为数组。所以你的填充属性会变成:

     populate: [{
                path: 'b',
                model: B
            },{
                path: 'c',
                model: C
            }]
    

    而查询是,

    A.find({_id:req.params.id})
        .populate({ path: 'patient', model: Patient,
            populate: [{
                path: 'b',
                model: B
            },{
                path: 'c',
                model: C
            }],
        })
        .exec(function (err, doc) {
            res.send(doc);
        })
    

    【讨论】:

    • 天哪!谢谢纳伊姆!我没注意到。
    • 为我节省了一些时间!! mongoosejs.com/docs/populate.html#deep-populate
    • 非常感谢人!我在那个用例上苦苦挣扎。我仍然认为我们可能会避免使用 mongoose,而是使用原生 nodejs mongodb 驱动程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 2019-08-14
    • 2016-09-03
    • 1970-01-01
    • 2018-12-12
    • 2017-07-06
    相关资源
    最近更新 更多