0
我有问题,在猫鼬的相关模式: 我有3层架构:更改猫鼬架构视图模型
var recipeProductSchema = new mongoose.Schema({
product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' },
productWeight: Number
}, {toJSON: {virtuals: true}});
var recipeSchema = new mongoose.Schema({
name: String,
products: [RecipeProduct]
})
var productSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
weight: {
type: Number,
required: true
},
price: {
type: Number,
required: true
}
})
当我想创建一个产品配方我送:
{
"name": "Cake",
"products": [
{
"product": "597a517b7c2e8508c8cc3f3a",
"productWeight": 50
}
]
}
但是,当我得到关于食谱的详细信息我得到这个:
{
"_id": "597f7d0ce25d1413149aa30d",
"name": "Cake",
"products": [
{
"product": {
"_id": "597a517b7c2e8508c8cc3f3a",
"name": "Sugar",
"weight": 400,
"price": 40
},
"productWeight": 50,
"_id": "597b825a61736b1adcb0ed0f",
"productPrice": "5.00",
"id": "597b825a61736b1adcb0ed0f"
}
]
}
是否有可能使这两个json相似吗? 因为在编辑配方视图中,我不想在数据发送之前更改配方的产品对象;
我想给这个JSON:
{
"name": "Cake",
"products": [
{
"product": {
"id": "597a517b7c2e8508c8cc3f3a" // or "_id"
},
"productWeight": 50
}
]
}
而且我想获得:
{
"_id": "597f7d0ce25d1413149aa30d",
"name": "Cake",
"products": [
{
"product": {
"_id": "597a517b7c2e8508c8cc3f3a",
"name": "Sugar",
"weight": 400,
"price": 40
},
"productWeight": 50,
"_id": "597f7d0ce25d1413149aa30e",
"productPrice": "5.00",
"id": "597f7d0ce25d1413149aa30e"
}
]
}