In the given data below, I am looking to remove a specific 'answers' object nested within the Doc without knowing the index of the question or answer. The text of the answer that needs deletion is available, but not the exact location in the arrays.
For instance, if the question text I need to pinpoint is "This is a question." and the answer to be removed is "Answer One", how can this task be accomplished?
Here's an example of the MongoDB Doc: (Quizzes contain Questions; Questions consist of Answers)
{
name: "Sample Quiz",
categories: [
{ name: "testcategory1", description: "this is a test category" }
,{ name: "categoryTWO", description: "the second category" }
],
questions: [
{ text: "This is a question."
,answers: [
{text: "Answer One", affected_categories: "testcategory1"}
,{text: "Answer Two", affected_categories: "testcategory1"}
,{text: "Answer Three", affected_categories: "categoryTWO"}
]
}
,{ text: "This is the second question."
,answers: [
{text: "Mepho One", affected_categories: "testcategory1"}
,{text: "Answer Toodlydoo", affected_categories: "testcategory1"}
,{text: "Lehmen Sumtin", affected_categories: "categoryTWO"}
]
}
],
}
Deleting an item nested one level down (i.e., a question) was achieved using a query like:
Quizzes.update(
{ _id: quizID, 'questions.text': questionText },
{ $pull: { questions: {text: questionText }}}
);
(referenced here: http://docs.mongodb.org/manual/core/update/#Updating-ModifierOperations, under the section titled "Update an Element without Specifying Its Position")
An attempt was made to extend this method as follows:
Quizzes.update(
{ _id: quizID, 'answers.text': answerText },
{ $pull: { questions: {text: questionText {answers: {text: answerText }}}}}
);
However, the above approach did not yield successful results.
If you have any suggestions or solutions, please feel free to share them. Your assistance would be greatly valued.