如何深入猫鼬 [英] How to deep populate on mongoose

查看:104
本文介绍了如何深入猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用这篇文章作为参考,但是我想我搞砸了.

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

所以我得到了product.addons和product.description.item

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

我已经尝试过了,但是似乎出现了一个永恒的循环(它不是console.log:

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

推荐答案

您在正确的轨道上,只是第二个代码段中的简单错误.尝试一下,看看是否获得期望的结果:

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

这篇关于如何深入猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆