我有这个猫鼬的模式:

var wishListSchema = new Schema({
    items: [{ type: Schema.Types.ObjectId, ref: 'Items' }]
});
var wishList = mongoose.model('WishList', wishListSchema, 'WishList');


我还有其他猫鼬模式:

var itemSchema = new Schema({
    "name":String,
    "description":String
});
var item= mongoose.model('Items', itemSchema, 'Items');


还有这个:

var UserSchema = new Schema({
    "name":String,
    "surname":String,
    "wishList": { type: Schema.Types.ObjectId, ref: 'WishList' }
});


但是,当我从数据库中获取用户并尝试填充wishList和项时,项的结果为null

User.findOne({name:name})
    .populate({
        path:"wishList",
        populate: {
            path:"items"
        }
    })
    .exec(function(err, userDb){
        console.log(userDb);
    });


结果是:

{
    _id: "56f85549334635c01b19bc37",
    name: "AAA",
    surname: "BBB"
    wishList: {
        _id: "56f85549334635c01b19bc39",
        __v: 0,
        items: [ ]
    }
}

最佳答案

尝试这个:

User.findOne({name:name})
.populate({
    path:"wishList",
    select: "items"
})
.exec(function(err, userDb){
    console.log(userDb);
});

09-25 20:49