Suppose I have a model created like this:
const attendance = mongoose.Schema({
studentName: {type: String, required: true},
status: {type: Number, required: true}
});
const session = mongoose.Schema({
sessionName: {type: String, required: true},
attendances: {type: [attendance]}
});
const subjectSchema = mongoose.Schema({
name: {type: String, required: true},
faculty_ID: {type: String, required: true},
students: {type: [String]},
sessions: {type:[session]}
});
module.exports = mongoose.model('Subject', subjectSchema);
And a sample document from that looks like this:
{
students: [
"Student1",
"Student2",
"Student3"
],
_id: "5bd5a6f49097cd505cf6317a",
name: "subjectName",
faculty_ID: "5bd57383bbe10f4f6439ae43",
sessions: [
{
attendances: [ ],
_id: "5bd6c4de95ffe03480ee8796",
sessionName: "Session1"
},
{
attendances: [ ],
_id: "5bd7a9d2604b760004947833",
sessionName: "Session2"
},
{
attendances: [ ],
_id: "5bd7a9e4604b760004947834",
sessionName: "Session3"
}
]
}
What would be the best way to push a new element in one of the attendances array inside sessions?
Like I want to push this:
{
studentName: "Student1",
status: "Present"
}
into the attendances array in the object with sessionName: "Session1" so that the document would then look like this:
{
students: [
"Student1",
"Student2",
"Student3"
],
_id: "5bd5a6f49097cd505cf6317a",
name: "subjectName",
faculty_ID: "5bd57383bbe10f4f6439ae43",
sessions: [
{
attendances: [
{
studentName: "Student1",
status: "Present"
}
],
_id: "5bd6c4de95ffe03480ee8796",
sessionName: "Session1"
},
{
attendances: [ ],
_id: "5bd7a9d2604b760004947833",
sessionName: "Session2"
},
{
attendances: [ ],
_id: "5bd7a9e4604b760004947834",
sessionName: "Session3"
}
]
}
Also, if I'm already successful in pushing new elements on that array, how would I remove a specific element on that array?