3

I am trying delete a specific index from the questions array. I have followed the examples in these two posts:

In mongoDb, how do you remove an array element by its index

How to delete n-th element of array in mongodb

 var quiz =  {
                        quizName: "",
                        createdBy: "",
                        theme: "",
                        isPrivate: "",
                        expiringDate: "",
                        createdOn: "",
                        questions:[
                                   {question: "",
                                   index: "",
                                   answers: [
                                             answer: "",
                                             radioCorrect: "",
                                             index: ""
                                   ]},{
                                   question: "",
                                   index: "",
                                   answers: [
                                             answer: "",
                                             radioCorrect: "",
                                             index: ""
                                   ]}

                        ]

This is giving me a not specified error. I have tried similar variations of this.

'deleteQuiz': function(quizId, index1){ 


    Quizes.update({_id: quizId},{ $unset : {questions.index1: 1}})

    Quizes.update({_id: quizId},{ "$pull" : {"questions": null}}) }
Community
  • 1
  • 1
SpiritCode
  • 77
  • 6

1 Answers1

0

Create the update object prior to using in your update operation. You can use the square bracket notation to create it as follows:

'deleteQuiz': function(quizId, index1){ 
    var query = {"_id": quizId},
        update { "$unset": { } };

    update["$unset"]["questions."+index1] = 1;
    Quizes.update(query, update)

    /* or if using the $pull operator
    var query = {"_id": quizId},
        update = { "$pull": { } };

    update["$pull"]["questions."+index1] = null;
    Quizes.update(query, update)    
    */
}
chridam
  • 100,957
  • 23
  • 236
  • 235