您如何在下面的购物车数组中填充产品字段?
{
"_id": "5bbd1c6e2c48f512fcd733b4",
"cart": [
{
"count": 1,
"_id": "5bbd30ed9417363f345b15fc",
"product": "5ba93a6d5d907d9512e43b75"
}
{
"count": 2,
"_id": "5bbd30ed9417363f345babcc",
"product": "5ba93a6d5d2359512e43b75"
}
],
"totalItems": 0,
"name": "kellie"
}
架构为:
const usersSchema = new mongoose.Schema({
cart: [
{
product: {type: mongoose.Schema.Types.ObjectId, ref: 'product'},
count: {type: Number},
},
],
totalItems: {type: Number, default: 0},
totalPrice: {type: Number},
});
我试图研究一种没有运气的解决方案。
答案 0 :(得分:1)
我重现了您的架构(主要部分为cart.product
),并且没有通过常规populate.exec()
方法填充的问题:
var result = Author.findOne({ _id: "5bbd93a29fceda195bec8665" })
.populate("books.book")
.exec()
.then(result => {
console.log(result)
})
我的作者架构具有相同的card.product
东西,但它是books.book
:
var AuthorSchema = new Schema({
books: [{
book: { type: Schema.Types.ObjectId, ref: "Book" },
count: {type: Number}
}
]
}
与Author.find({})
等相同的情况。