【发布时间】:2019-02-24 05:25:01
【问题描述】:
我发现这篇文章非常接近我的需要,但不知何故我仍然无法让它工作
Populate nested array in mongoose
解释我说的是哪种嵌套 ref 有点困难。我只是从代码开始
我有一个产品架构
const ProductSchema = new Schema(Object.assign({
name: {type: String};
});
订单模式
const OrderSchema = new Schema(Object.assign({
products: [ {
product: { type: Schema.Types.ObjectId, ref: 'Products' },
amount: { type: Number },
total: { type: Number },
} ],
});
我试过了
const order = await Orders.findOne({
_id: 'orderId39843984203'
}).populate({
path: 'products',
populate: {
path: 'product'
}
});
我尝试过类似的方法,以及其他一些方法,例如 path: products.product 或 path: products.product._id 以及类似的方法
但我只能得到_id,它并没有填充整个内容。
有人可以帮我一把或建议如何工作吗?
提前致谢
编辑:这是 orderSchema 中文档在 db 中的样子
{
"_id": {
"$oid": "5ba2e2af52f2ff3f4226015c"
},
"products": [
{
"_id": {
"$oid": "5ba2e2ac52f22f3f4226015e"
},
"amount": 4,
"total": 2940
},
{
"_id": {
"$oid": "5ba2e2ac52f2ff3f5226015d"
},
"amount": 1,
"total": 840
}
],
"createdAt": {
"$date": "2018-09-19T23:58:36.339Z"
},
"updatedAt": {
"$date": "2018-09-19T23:58:36.505Z"
},
"__v": 0
}
【问题讨论】:
标签: javascript mongodb mongoose mongoose-schema mongoose-populate