Home > Net >  Update an object inside nested array using mongoose
Update an object inside nested array using mongoose

Time:11-04

My mongo schema looks like this, I want to update a flashcard object located in an array of flashcard also located in 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.

CodePudding user response:

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

CodePudding user response:

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);
  • Related