1

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?

1 Answer 1

2

You can use the $ positional operator to indicate which session should be modified. Try:

Model.updateOne({ _id: "5bd5a6f49097cd505cf6317a", "sessions._id": "5bd6c4de95ffe03480ee8796" }, 
    { $push: { "sessions.$.attendances": {  studentName: "Student1", status: "Present" } } })

To remove the value you need the same operator and $pull which takes condition as a parameter, for instance:

Model.updateOne({ _id: "5bd5a6f49097cd505cf6317a", "sessions._id": "5bd6c4de95ffe03480ee8796" }, 
    { $pull: { "sessions.$.attendances": {  studentName: "Student1" } } })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.