我必须以以下格式生成JSON-
"products": {
"1": {
"product_id": "12",
"quantity": "1",
"product_options": {
"1": "12",
"2": "17"
}
},
"2": {
"product_id": "12",
"quantity": "2",
"product_options": {
"1": "15",
}
}
}
如何在猫鼬模式中做到这一点?目前,我的架构是-
const mongoose = require('mongoose');
const timestamp = require('mongoose-timestamp');
const ProductOptionSchema = new mongoose.Schema({
product_option_id: { type: String, required: true }
});
const OrderedProductSchema = new mongoose.Schema({
product_id: { type: String, required: true },
quantity: { type: Number, required: true },
product_options: [ProductOptionSchema]
});
OrderedProductSchema.plugin(timestamp);
const ProductOption = mongoose.model('ProductOption', ProductOptionSchema);
const OrderedProduct = mongoose.model('OrderedProduct', OrderedProductSchema);
module.exports = Order;
不会产生所需格式的JSON。我可以手动构建JSON使其看起来像所需的输出,但我希望尽可能不必双重处理JSON,只需创建一个猫鼬模式即可首次生成所需的输出。
谢谢!