如何基于其ObjectId删除嵌套对象? [英] How to delete an nested object based on its ObjectId?

查看:114
本文介绍了如何基于其ObjectId删除嵌套对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的课程集合具有此嵌套模式,每个课程中都有一个课程数组,每个课程中都有一个学生数组,每个学生都是一个对象,该对象由一个userName属性和一个ObjectId值组成,该对象引用我的用户集合,另一个属性名称状态包含某个数字.

I have this nested Schema for my courses collections, there is a sessions array in every course and a students array in every session and every student is an object consisting of a property of userName with a value of ObjectId refering to my users collections and another property names status containing some number.

我想从我的会话的学生"数组中删除其_id的学生对象.

I want to delete an student object from my students array of my session with its _id.

我知道可以放宽对单个对象的控制,但是我需要一种整洁的方式,例如使用objectId从数据库中删除对象,这样我们就不必指定路径,例如直接删除或修改嵌套的对象子文档.

I know it is possible to unwind arrray to get to a single object but I need a neat way like using an objectId to delete an object from database so that we don't have to specify path like directly deleting or modifying that nested subdocument.

这是我的课程模式:

 CourseSchema = new mongoose.Schema({
    name:String,
    sessions:[
        {
         date:Date,
         students :[{
             userName:{
                type:mongoose.Schema.Types.ObjectId,
                ref :'users'
             },
             status:Number
         }]   
        }
    ]
})

推荐答案

您可以使用以下DELETE路线从课程中删除学生.

You can use the following DELETE route to delete a student from a course session.

router.delete(
  "/course/:courseId/session/:sessionId/student/:studentId",
  async (req, res) => {
    try {
      let result = await Course.updateOne(
        { _id: req.params.courseId, "sessions._id": req.params.sessionId },
        {
          $pull: { "sessions.$.students": { userName: req.params.studentId } }
        }
      );

      res.send(result);
    } catch (err) {
      console.log(err);
      res.status(500).send("Something went wrong");
    }
  }
);

假设您有一门这样的课程:

Let's say you have a course like this:

{
    "_id" : ObjectId("5de907acdfcef9493cd215a8"),
    "name" : "Course 1",
    "sessions" : [
        {
            "date" : ISODate("2019-12-05T16:32:41.998+03:00"),
            "_id" : ObjectId("5de907acdfcef9493cd215a9"),
            "students" : [
                {
                    "_id" : ObjectId("5de907acdfcef9493cd215ac"),
                    "userName" : ObjectId("5de8e4c8f74cf254d04f90d8"),
                    "status" : 1
                },
                {
                    "_id" : ObjectId("5de907acdfcef9493cd215ab"),
                    "userName" : ObjectId("5de8e4d5f74cf254d04f90d9"),
                    "status" : 1
                },
                {
                    "_id" : ObjectId("5de907acdfcef9493cd215aa"),
                    "userName" : ObjectId("5de8e4ddf74cf254d04f90da"),
                    "status" : 1
                }
            ]
        }
    ],
    "__v" : 0
}

如果我们要删除用户名值为5de8e4ddf74cf254d04f90da的学生,则可以使用以下网址将DELETE请求发送到我们的路线:

If we want to delete the student with userName with value 5de8e4ddf74cf254d04f90da, we can send a DELETE request to our route using an url like this:

http://localhost/courses/5de907acdfcef9493cd215a8/session/5de907acdfcef9493cd215a9/student/5de8e4ddf74cf254d04f90da

5de907acdfcef9493cd215a8-> courseId

5de907acdfcef9493cd215a8--> courseId

5de907acdfcef9493cd215a9-> sessionId

5de907acdfcef9493cd215a9--> sessionId

响应如下:

{
    "n": 1,
    "nModified": 1,
    "ok": 1
}

当我们看我们的课程时,我们看到该学生已被撤职:

When we look at our course, we see the student is removed:

{
    "_id" : ObjectId("5de907acdfcef9493cd215a8"),
    "name" : "Course 1",
    "sessions" : [
        {
            "date" : ISODate("2019-12-05T16:32:41.998+03:00"),
            "_id" : ObjectId("5de907acdfcef9493cd215a9"),
            "students" : [
                {
                    "_id" : ObjectId("5de907acdfcef9493cd215ac"),
                    "userName" : ObjectId("5de8e4c8f74cf254d04f90d8"),
                    "status" : 1
                },
                {
                    "_id" : ObjectId("5de907acdfcef9493cd215ab"),
                    "userName" : ObjectId("5de8e4d5f74cf254d04f90d9"),
                    "status" : 1
                }
            ]
        }
    ],
    "__v" : 0
}

我们看到,用户名值为5de8e4ddf74cf254d04f90da的学生在课程中不再存在,这意味着该用户已被删除.

As we see the student with username with value 5de8e4ddf74cf254d04f90da does not exists anymore in the course session, meaning it is removed.

这篇关于如何基于其ObjectId删除嵌套对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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