我已经使用post here文章作为参考,但我想我已经搞砸了。
我想深入填充模型Cart:
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
});
所以我得到product.addons和product.description.item
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();
});
我已经尝试了这个,但它似乎继续进行一些永恒的循环(它没有控制台.log:
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'
}
但它并没有让我得到我想要的结果。
我希望我的结果看起来像这样:
Rate.where('A = 1 and B = 2')
答案 0 :(得分:1)
您走在正确的轨道上,只是第二个代码段中的简单错误。试试这个,看看你是否得到了预期的结果:
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){
...
});