How to deep populate on mongoose

228 views Asked by At

I've used this article as reference, but I guess I have screwed something up.

I want to deep populate the model Cart:

var CartSchema = new Schema({
  products: [{
    product: { type: Schema.ObjectId, ref : 'Product' },
    quantity: { type: Number, default: 1}
  }],
  totalItems: { type: Number, default: 0},
  message: { type: String },
  client: { type : Schema.ObjectId, ref : 'User' },
  time: { type: Date, default: new Date()},
  session: { type: String }
});

So I get product.addons and product.description.item

var ProductSchema = new Schema({
  name: { type: String, default: '' },
  inventory: { type: Number }, 
  type: { type: String, default: 'cesta' }, 
  price: { type: Number, required: true }, 
  discount: { type: Number}, 
  description: [{
    item: { type : Schema.ObjectId, ref : 'Item' },
    quantity: Number
  }], 
  photos: {
    photo1: String,
    photo2: String
  },
  related_products: [{ type : Schema.ObjectId, ref : 'Product' }],
  addons: [{ type : Schema.ObjectId, ref : 'Product' }],
  category: { type: Schema.ObjectId, ref: 'Category' },
  code: { type: String }, 
  descricao_avulsa: String, 
  slug: String
});

I've tried this, but it seems to go on some eternal loop (it doesn't console.log:

var populate = {
    path: "products.product",
    model: 'Product',
    populate: [
        { path: "price name photos slug" },
        { path: "description.item addons", model: 'Item'}
    ]
};
Cart.findOne({session: req.cookies['express:sess']})
  .populate(populate)
  .exec(function(err, cart){
    if(err){
      return err;       }
    console.log(cart.products[0].product);

    next();
  });

I've also tried the same code, with this for the populate variable:

var populate = [
    { path: "products.product", select: "price name photos slug description addons" },
    { path: "products.product.description.item", select: "name" },
    { path: "products.product.addons", select: "name" }
];

But it doesn't get me the results I want.

I want my result to look something like this:

{
  _id: 5859790cc307556218b9d2e1,
  slug: 'nova-cestinha',
  price: 14300,
  addons: [ { name: 'produto' } ],
  photos: {
    photo1: 'https://frutacor.s3.amazonaws.com/1482258691162',
    photo2: 'https://frutacor.s3.amazonaws.com/1482258691189'
  },
  description: 
    [ { 
       item: {name: 'casadinho'},
       quantity: 4,
       _id: 5859790cc307556218b9d2e4
     },
     {
       item: {name: 'brownie},
       quantity: 5,
       _id: 5859790cc307556218b9d2e3 }
   ],
 name: 'nova cestinha'
}
1

There are 1 answers

3
Ravi Shankar Bharti On BEST ANSWER

You are on the right track, just simple mistakes in your second code snippet. Try this and see if you get the desired results:

Cart.findOne({session: req.cookies['express:sess']})
.populate({
    path : 'products.product',
    select : 'price name photos slug description addons',
    populate : [{ path : 'description.item' , select : 'name', model: 'Item'},
        { path : 'addons',select : 'name'}]
})
.exec(function(err, cart){
...
});