我有一个json doc,它嵌入了可变的文档(但是以json格式)。所以我使用Mixed Schema类型。 当我保存时,一切正常,除了混合类型对象没有填充并且不会保存。 我在这做错了什么? 正在更新 - >我的意思是 - 一切都按预期工作,除了数据节点(假设是混合类型)
My Document Example:
{
"data": {
"user_name": "username",
"cart_items": [
{
"sku": "ABCD",
"msrp": 1250.25,
"discount": 10,
"final_price": 112.22
},
{
"sku": "PQRSDF",
"msrp": 12.25,
"discount": 10,
"final_price": 1.2
}
]
},
"template_id": "1",
"from": "x@gmail.com",
"send_status": 0,
"priority": 99,
"app_id": "app3",
"_id": "532a54aa1c76fba0874c48ea",
"bcc": [],
"cc": [],
"to": [
{
"name": "acv",
"email": "x@outlook.com"
},
{
"name": "pem",
"email": "y@gmail.com"
}
],
"call_details": {
"data_id": "01234",
"event_id": 25
}
}
code to insert:
Schema definition:
app_id : { type: String, trim: true },
priority: { type: Number},
send_status: { type: Number},
call_details : {
event_id : { type: Number},
data_id : { type: String, trim: true },
id : false
},
from : { type: String, trim: true },
to : [addressSchema],
cc : [addressSchema],
bcc : [addressSchema],
template_id : { type: String, trim: true },
data: { any: {} }
Code:
r.app_id = req.body.app_id;
r.priority= req.body.priority;
r.send_status= req.body.send_status;
r.call_details.event_id= req.body.call_details.event_id;
r.call_details.data_id= req.body.call_details.data_id;
r.from= req.body.from;
r.to = populate_address(req.body.to);
r.cc = populate_address(req.body.cc);
r.bcc = populate_address(req.body.bcc);
r.template_id= req.body.template_id;
r.data =req.body.data);
r.markModified('data');
r.save(function (err){
console.log("add");
res.send ("added");
});
答案 0 :(得分:1)
在您当前定义架构时,它只会在any
中保存data
字段。
从架构中any
的定义中删除data
嵌入字段。
所以而不是:
data: { any: {} }
使用:
data: {}
答案 1 :(得分:0)
由于mongoose无法自动处理嵌入式文档。您需要先保存嵌入的文档,然后将对象ID指定给父模式作为参考。