更新猫鼬中的嵌套子文档 [英] Update nested subdocument in Mongoose

查看:70
本文介绍了更新猫鼬中的嵌套子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ExpressJS更新Mongoose中的嵌套子文档,但似乎无法正常工作.位置$运算符的遍历不超过1个级别,我尝试使用arrayFilters进行操作,但似乎也无法正常工作.

I'm trying to update a nested subdocument in Mongoose using ExpressJS, but I can't seem to get it working. The positional $ operator does not traverse more than 1 level, and I've tried using arrayFilters but I can't seem to get that working either.

这是文档:

{
    "_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);

推荐答案

我不知道我是否对如何准确解决您的问题有任何答案,但是我将推荐一种解决方案,我认为它会大大提高您的水平您的代码中的组织机构...

I don't know if I have an answer on how to exactly fix your problem, but I am going to recommend a solution that I think significantly increases the level of organization in your code...

(1)首先,每个模式都应导出为单独的模型(我建议它们分别在单独的文件中)

(1) First and foremost, each of those schemas should be exported as separate models (and I would recommend them each be in separate files)

(2)其次,在您的subitemsmenuItems模式中,创建消息和子项引用.如果您遵循我的第一个建议,则可以使menuItems模式如下:

(2) Second, in your subitems and menuItems schemas, make the messages and subitems references. If you follow the first suggestion I made, then you can make your menuItems schema like follows:

menuItems.js

const menuItems = new mongoose.Schema({
  title : String,
  subitem: {
    type: mongoose.Types.ObjectId,
    ref: 'sub_items'
  }
});

module.exports = mongoose.model("menu_items", menuItems);

其中sub_items是已导出的子项目模型的名称.在您的子项目模型中也使用类似的格式,但是我会让您这样做,因为我不想偷所有的乐趣;)

Where sub_items is the name of your subitems model which you have exported. Use a similar format with your subitems model too, but I'll let you do that because I don't want to steal all the fun ;)

(3)现在,这些都是引用,当您需要更改menuItem引用的子项目时,只需要更新实际的子项目即可.您不再需要通过菜单项对象 来更新子项,因为对子项的引用永远不会改变!

(3) Now that these are references, when you need to change a subitem that a menuItem references, you just need to update the actual subitem. You no longer need to update the subitem through the menuItem object, since the reference to the subitem never changes!

我希望这会有所帮助.现在,它应该更加井井有条,并且非常更容易更新您的子项和子消息

I hope this helps. It should be much more organized for you now, and should be MUCH simpler to updated your subitems and submessages

这篇关于更新猫鼬中的嵌套子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆