在mongoose中填充嵌套数组

时间:2017-12-16 13:56:55

标签: node.js mongodb mongoose populate

我正在填充一个字符数组,每个字符都有它的类和种族作为对象的引用。什么是正确的填充查询来获得,种族和类。谢谢你的回答

UserModel.
findOne({ Something: 'Something' })
populate({
    path: "characters",
    populate:{
        path: "class, race"
    }
})
.exec(function(err, docs) {
    console.log(docs.characters[1]);
    console.log(err);
});

- 更改了模型的结尾,现在可以正常工作。

UserModel.
    findOne({ Something: 'Something' })
    .populate({
        path: "characters",
        model: 'Characters',
        populate: [{
            path: 'class',
            model: 'Classes'
        },
        {
            path: 'race',
            model: 'Races'
        }]
    })
    .exec(function (err, docs) {
        console.log(docs.characters[1]);
        console.log(err);
    });

1 个答案:

答案 0 :(得分:0)

试试这个:

UserModel.
    findOne({ Something: 'Something' })
    .populate({
        path: "characters",
        model: 'Character',
        populate: [{
            path: 'class',
            model: 'Class'
        },
        {
            path: 'race',
            model: 'Race'
        }]
    })
    .exec(function (err, docs) {
        console.log(docs.characters[1]);
        console.log(err);
    });