【问题标题】:How to remove item in nested collection by id - mongoose如何按 id 删除嵌套集合中的项目 - mongoose
【发布时间】:2021-04-23 12:12:04
【问题描述】:

我想通过 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 方法,我可以轻松地从该区域移除路线。但我无法删除更多嵌套的项目,如章节和主题。

我该如何解决这个问题?

【问题讨论】:

    标签: mongodb express mongoose


    【解决方案1】:

    您可以像这样使用 arrayFilters 来做到这一点:

    db.collection.updateOne(
    {
        _id: ObjectId("607f7c67f64e993a5c9b9793")
    },
    {
        $pull: {
            "subjects.$[s].courses.$[c].chapters.$[ch].topics": {
                _id: ObjectId("607f7ccef64e993a5c9b979b")
            }
        }
    },
    {
        arrayFilters: [
            {
                "s._id": { $eq: ObjectId("607f7c7af64e993a5c9b9794") }
            },
            {
                "c._id": { $eq: ObjectId("607f7c9af64e993a5c9b9796") }
            },
            {
                "ch._id": { $eq: ObjectId("607f7cb4f64e993a5c9b9798") }
            }
        ]
    })
    

    https://mongoplayground.net/p/kVyV27nnkII

    【讨论】:

      【解决方案2】:

      试试看:

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

      【讨论】:

        猜你喜欢
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多