问题
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.
I want to delete an student object from my students array of my session with its _id.
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.
This is my course schema:
CourseSchema = new mongoose.Schema({
name:String,
sessions:[
{
date:Date,
students :[{
userName:{
type:mongoose.Schema.Types.ObjectId,
ref :'users'
},
status:Number
}]
}
]
})
回答1:
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
}
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
5de907acdfcef9493cd215a9--> sessionId
The response will be like this:
{
"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
}
As we see the student with username with value 5de8e4ddf74cf254d04f90da
does not exists anymore in the course session, meaning it is removed.
来源:https://stackoverflow.com/questions/59193168/how-to-delete-an-nested-object-based-on-its-objectid