'Mongoose - Update an object inside nested array

My Mongo schema looks like this, I want to update a flashcard object located in an array of flashcard also located in an array of subject.

const classrooms = new mongoose.Schema({
    name: String,
    year: String,
    student: [
        {
            firstname: String,
            lastname: String,
            mail: String,
            userId: String,
        }
    ],
    subject: [
        {
            subjectId: String,
            flashcard: [
                {
                    title: String,
                    tag: []
                }
            ]
        }
    ]
});

What I am doing is

const flashcard = await classroomModel.findOneAndUpdate({
    _id : classroomId,
    "subject" : {
        "subjectId" : subjectId,
        "subject.flashcard" : {
            "_id" : flashcardId
        }
    },
    "$set" : {
        "flashcard.title" : "new title"
    }
})

But this is deleting all flashcards located inside an object. Any help would be appreciated.



Solution 1:[1]

You need arrayFilters to specify the (to-be-updated) flashcard document that meets the criteria for subject and flashcard.

db.collection.update({
  _id: 1//classroomId,
  
},
{
  "$set": {
    "subject.$[subject].flashcard.$[flashcard].title": "new title"
  }
},
{
  arrayFilters: [
    {
      "subject.subjectId": 1//subjectId
      
    },
    {
      "flashcard._id": 1//flashcardId
      
    }
  ]
})

Sample Mongo Playground

Solution 2:[2]

Although i accepted @yong shun which is the best approach, another way to do it in case you don't like mongoose syntax :


      const classroom = await classroomModel.findOne(
        {
          _id: 1 //classroomId,
        },
      )

      const subject = classroom.subject.find(
        (currentSubject: any) => {
          return currentSubject.subjectId == 1 //subjectId
        }
      )

      const flashcard = subject.flashcard.find(
        (currentFlashcard: any) => {
          return currentFlashcard._id == 1 //flashcardId
        }
      )
      flashcard.title = "my new title";
      await classroomModel.updateOne(classroom);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yong Shun
Solution 2 SirageDb