Dav*_* C. 2 javascript mongodb meteor
我需要删除嵌入在下面的Doc中的一个"答案"对象.我有我正在寻找答案的文字.我没有问题的索引或我需要深入研究数组的答案.
例如,我知道我试图深入研究的问题是"这是一个问题".我想删除的答案是"答案一".
你会怎么做呢?
以下是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"}
]
}
],
}
Run Code Online (Sandbox Code Playgroud)
当我删除嵌套在单个级别的项目时(在这种情况下,一个问题),我能够使用如下查询来执行此操作:
Quizzes.update(
{ _id: quizID, 'questions.text': questionText },
{ $pull: { questions: {text: questionText }}}
);
Run Code Online (Sandbox Code Playgroud)
(如下所述:http://docs.mongodb.org/manual/core/update/#Updating-ModifierOperations,标题为"更新元素而不指定其位置"一节)
我尝试将其扩展为:
Quizzes.update(
{ _id: quizID, 'answers.text': answerText },
{ $pull: { questions: {text: questionText {answers: {text: answerText }}}}}
);
Run Code Online (Sandbox Code Playgroud)
但没有任何运气.
任何想法将不胜感激.
将位置运算符与$ pull条件结合使用:
> db.quizzes.update(
{_id:<quizID>, "questions.text":"This is the second question."},
{$pull:{ "questions.$.answers":{"text":"Answer Toodlydoo"}}}
);
Run Code Online (Sandbox Code Playgroud)
以上工作从第二个问题中删除第二个答案.
| 归档时间: |
|
| 查看次数: |
2023 次 |
| 最近记录: |