我需要删除下面文档中嵌套的“答案”对象之一。我有我正在寻找的答案的文本。我没有问题的索引或需要深入研究数组的答案。
例如,我知道我要深入研究的问题的文本是“这是一个问题”。我要删除的答案是“答案一”。
你会怎么做呢?
这是示例 MongoDB Doc:(测验有问题;问题有答案)
{
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"}
]
}
],
}
当我删除一个嵌套在单个级别下的项目时(在本例中是一个问题),我可以使用如下查询来执行此操作:
Quizzes.update(
{ _id: quizID, 'questions.text': questionText },
{ $pull: { questions: {text: questionText }}}
);
(如此处所述:http: //docs.mongodb.org/manual/core/update/#Updating-ModifierOperations,在标题为“更新元素而不指定其位置”的部分中)
我尝试将其扩展为:
Quizzes.update(
{ _id: quizID, 'answers.text': answerText },
{ $pull: { questions: {text: questionText {answers: {text: answerText }}}}}
);
但没有任何运气。
任何想法将不胜感激。