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

1 Answer 1

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)    
    */
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you chridam, it worked like charm! I just had to put a = sign after update.
@SpiritCode No worries, happy to help :) Left out the = as a typo, its been corrected now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.