首页 > 解决方案 > 如何按 id 删除嵌套集合中的项目 - mongoose

问题描述

我想通过 id 从以下集合中删除一个主题。这是集合的层次结构 - 区域。

Area ---
     |
     ---Subjects---
                 |
                 Courses---
                          |
                          Chapters----
                                     |
                                     Topics

区域示例如下。

{
    "_id" : ObjectId("607f7c67f64e993a5c9b9793"),
    "name" : "Automatic Engineering",
    "description" : "this is the description.",
    "created" : ISODate("2021-04-21T01:14:15.736Z"),
    "subjects" : [ 
        {
            "description" : "dddd",
            "name" : "Transfer Function",
            "_id" : ObjectId("607f7c7af64e993a5c9b9794"),
            "created" : ISODate("2021-04-21T01:14:34.943Z"),
            "courses" : [ 
                {
                    "description" : "dddd",
                    "name" : "conception",
                    "_id" : ObjectId("607f7c9af64e993a5c9b9796"),
                    "created" : ISODate("2021-04-21T01:15:06.696Z"),
                    "chapters" : [ 
                        {
                            "description" : "chapter 1",
                            "name" : "Chapter 1",
                            "_id" : ObjectId("607f7cb4f64e993a5c9b9798"),
                            "created" : ISODate("2021-04-21T01:15:32.855Z"),
                            "topics" : [ 
                                {
                                    "description" : "1",
                                    "name" : "Topic 1",
                                    "_id" : ObjectId("607f7ccef64e993a5c9b979b"),
                                    "created" : ISODate("2021-04-21T01:15:58.064Z")
                                }, 
                                {
                                    "description" : "2",
                                    "name" : "Topic 2",
                                    "_id" : ObjectId("607f7cd5f64e993a5c9b979c"),
                                    "created" : ISODate("2021-04-21T01:16:05.663Z")
                                }, 
                                {
                                    "description" : "3",
                                    "name" : "Topic 3",
                                    "_id" : ObjectId("607f7cdcf64e993a5c9b979d"),
                                    "created" : ISODate("2021-04-21T01:16:12.336Z")
                                }
                            ]
                        }, 
                        {
                            "description" : "chapter2",
                            "name" : "Chapter 2",
                            "_id" : ObjectId("607f7cbcf64e993a5c9b9799"),
                            "created" : ISODate("2021-04-21T01:15:40.231Z"),
                            "topics" : []
                        }, 
                        {
                            "description" : "chapter 3",
                            "name" : "Chapter 3",
                            "_id" : ObjectId("607f7cc5f64e993a5c9b979a"),
                            "created" : ISODate("2021-04-21T01:15:49.000Z"),
                            "topics" : []
                        }
                    ]
                }, 
                {
                    "description" : "dddd",
                    "name" : "exercise",
                    "_id" : ObjectId("607f7ca6f64e993a5c9b9797"),
                    "created" : ISODate("2021-04-21T01:15:18.465Z"),
                    "chapters" : []
                }
            ]
        }, 
        {
            "description" : "this is the example",
            "name" : "State Space Equation",
            "_id" : ObjectId("607f7c8ef64e993a5c9b9795"),
            "created" : ISODate("2021-04-21T01:14:54.056Z"),
            "courses" : []
        }
    ],
    "__v" : 0
}

我需要删除章节数组中的主题对象。所以这就是我所做的。

areaModel.findOneAndUpdate({
    '_id': req.body.area_id,
    'subjects._id': req.body.subject_id,
    'courses._id': req.body.course_id,
    'chapter._id': req.body.chapter_id
}, {
    "$pull": {
        "subjects.$.courses.$.chapters.$.topics": {
            _id: req.body.topic_id
        }
    }
},  (err, result) =>{
    if (err) {
        return res.json({success: false, msg: err});
    } else {
        return res.json({success: true, msg:"successfully removed" });
    }
});

通过使用 mongoose 的 $pull 方法,我可以轻松地将课程从该区域移除。但是我不可能删除更多嵌套的项目,如章节和主题。

我该如何解决这个问题?

标签: mongodbexpressmongoose

解决方案


尝试一下:

areaModel.findOneAndUpdate(
  { _id: req.body.area_id },
  {
    $pull: {
      "subjects.$[].courses.$[].chapters.$[].topics": {
        _id: req.body.topic_id,
      },
    },
  }
);

推荐阅读