简体   繁体   中英

Update an array in a sub-document - mongoose mongodb

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?

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" } } })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM