Unable to update my collection

** I have this collection which I want to update without affectin any previous data **

{
  "semester": "4",
  "subjectData": {
    "subjectName": "english",
    "questionBank": {
      "question": "who are you?",
      "answer": {
        "introduction": "Hello my name is BHoola",
        "features": [
          "tall",
          "handsome"
        ],
        "kinds": [
          "very",
          "kind"
        ],
        "conclusion": "Mighty man",
        
      }
    }
  }
}

** Currently I am trying to do this with** const insertResult = await semesterData.updateMany( { $and: [ { semester: semesterRecieved, 'subjectData.subjectName': subject, }, ], }, { $set: { 'subjectData.questionBank.question': questionRecieved, 'subjectData.questionBank.answer': answerRecieved, }, }, { upsert: false }, );
** to which I am getting acknowledge: false **

** I want my collection to look like this **

    "semester": "4",
    "subjectData": {
      "subjectName": "english",
      "questionBank": {
        "question": "why are you so tall?",
        "answer": {
          "introduction": "Hello my name is 3333",
          "features": [
            "tall",
            "handsome"
          ],
          "kinds": [
            "very",
            "kind"
          ],
          "conclusion": "Mighty man",
          
        },
            
        },
        {
            "question": "who are you?",
            "answer": {
              "introduction": "Hello my name is 33333",
              "features": [
                "tall",
                "handsome"
              ],
              "kinds": [
                "very",
                "kind"
              ],
              "conclusion": "Mighty man",
              
            },
                
          }
    }
  }```

Hello @Izaan_Anwar,

Your expected result is not valid JSON. Can you please provide valid JSON for what you are looking for?

oh yeah I fixed it it was very trivila I just added an array in questionBank and I am keeping question and answers in an object and when I post new data it goes in that array making a new object.

1 Like

So I had to make the questionBank object into an array containing onjects and I used collection.update with $addToSet and it worked!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.