Lorenzo
Lorenzo

Reputation: 225

Mongoose population

I have this Mongoose Schema

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

and the schema of the review is:

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

and the schema of the users is:

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

This is the code to get the item:

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

But this code only populates the reviews, and I want that to populate also the user.

Upvotes: 2

Views: 84

Answers (1)

war1oc
war1oc

Reputation: 2755

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

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

Upvotes: 4

Related Questions