How to deep populate on mongoose

╄→гoц情女王★ 提交于 2019-12-25 04:37:31

问题


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:


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){
...
});


来源:https://stackoverflow.com/questions/41255313/how-to-deep-populate-on-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!