猫鼬人口

时间:2016-03-30 16:22:12

标签: javascript node.js mongodb mongoose

我有这个Mongoose Schema

var ItemSchema = new Schema({
    "name":String,
    "review": [{ type: Schema.Types.ObjectId, ref: 'Reviews'}]
});

并且评论的架构是:

var ReviewSchema = new Schema({
    "title": String, 
    "user": { type: Schema.Types.ObjectId, ref: 'Users' }
});

用户的架构是:

var UserSchema = new Schema({
   "name":String, 
   "surname":String,
});

这是获取项目的代码:

Item.findOne({_id:req.params.idItem})
    .populate('review')
    .exec(function (err, item) {
        console.log(item);
});

但是这段代码只会填充评论,我希望这也填充用户。

1 个答案:

答案 0 :(得分:4)

Item.findOne({_id:req.params.idItem})
  .populate({ 
     path: 'review',
     populate: {
       path: 'user'
     } 
  })
  .exec(function(err, item) {});

http://mongoosejs.com/docs/populate.html#deep-populate