【问题标题】:How to deep populate on mongoose如何在猫鼬上进行深度填充
【发布时间】:2017-05-06 10:28:24
【问题描述】:

我使用了this 文章作为参考,但我想我搞砸了。

我想深入填充模型购物车:

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

所以我得到了 product.addons 和 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
});

我已经尝试过了,但它似乎一直在循环(它没有 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();
  });

我也尝试了相同的代码,其中填充变量:

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" }
];

但这并没有得到我想要的结果。

我希望我的结果看起来像这样:

{
  _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'
}

【问题讨论】:

    标签: mongoose mongoose-populate


    【解决方案1】:

    您在正确的轨道上,只是您的第二个代码 sn-p 中的简单错误。试试这个,看看你是否得到了想要的结果:

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

    【讨论】:

    • 它适用于插件。我会试着弄清楚description.item,谢谢你的帮助
    • 它应该对两者都有效,但我不知道填充中的多个填充,这可能导致了问题,否则它应该有效。
    • 我在 description.item 中添加了 "model: 'Item'" 并且它起作用了,但不确定为什么。我不太确定在 stackoverflow 上进行此操作的最佳方法,因此我尝试编辑您的答案,使其正确,我可以接受它
    猜你喜欢
    • 1970-01-01
    • 2015-02-09
    • 2015-09-26
    • 2018-01-30
    • 1970-01-01
    • 2015-07-13
    • 2016-10-10
    • 2019-12-01
    相关资源
    最近更新 更多