【发布时间】:2019-02-05 00:15:07
【问题描述】:
我正在尝试使用 ExpressJS 更新 Mongoose 中的嵌套子文档,但我似乎无法使其正常工作。位置 $ 运算符不会遍历超过 1 个级别,我尝试过使用 arrayFilters 但我似乎也无法使其正常工作。
这是文件:
{
"_id" : ObjectId("5c52b017d9133d14876d2493"),
"title" : "main_item",
"subitem" : [
{
"_id" : ObjectId("5c52f4e74ef7482a646fc264"),
"title" : "sub_item",
"messages" : [
{
"_id" : ObjectId("5c5303fa4b20df2d33d6eb08"),
"type" : "single_article",
"date" : "Jan 31, 2019",
"title" : "item",
"text" : "<p>item text</p>"
},
{
"_id" : ObjectId("5c53040b4b20df2d33d6eb09"),
"type" : "single_article",
"date" : "Jan 31, 2019",
"title" : "item 2",
"text" : "<p>item 2 text</p>"
}
]
}
],
"__v" : 0
}
postController.updateMessage = function(req, res, item) {
var id = req.body.id;
var sub_id = req.body.sub_id;
var saveData = {
title: req.body.title,
text: req.body.text
};
item.updateOne({'subitem._id': id}, {$set: {'subitem.$[el1].messages.$[el2]': saveData}}, {arrayFilters:[{"el1._id": id},{"el2._id": sub_id}]})
};
猫鼬模型:
var submessages = mongoose.Schema({
date: String,
type: String,
title: String,
text: String
});
var subitems = new mongoose.Schema({
title: String,
messages: [submessages]
});
var menuItems = new mongoose.Schema({
title : String,
subitem: [subitems]
}, {collection: 'menu_items'});
module.exports = mongoose.model("menu_items", menuItems);
【问题讨论】:
-
你能分享你的模型吗?它可能有助于回答问题
-
我已经用我的模型编辑了它