我试图删除此数组中的以下dict:
{
"ProjectID": "8ToGTAUjT+CbcH68ZYuW8Q=="
"Tasks": [
{
"description": "",
"title": "testcounter",
"notes": "",
"percentComplete": "100",
"completedDate": {
"hour": "12",
"year": "2014",
"day": "14",
"minute": "43",
"month": "11"
},
"completed": "",
"ProjectID": "8ToGTAUjT+CbcH68ZYuW8Q==",
"TaskID": "JxHddpQNSguzOqg1sdsdKtyQ=="
},
......
],
......
}
Tasks
是此记录中的子文档。
我尝试删除:
db.projects.update({'ProjectID': "8ToGTAUjT+CbcH68ZYuW8Q=="},
{'$pull': {'Tasks.TaskID': "JxHddpQNSguzOqg1sdsdKtyQ=="}})
然而,这不会删除此子文档。如何删除数组中的dict?
答案 0 :(得分:9)
尝试使用此代码:
db.projects.update(
{'ProjectID': "8ToGTAUjT+CbcH68ZYuW8Q=="},
{'$pull': {Tasks:{ TaskID: "JxHddpQNSguzOqg1sdsdKtyQ=="}}}
)
$pull
表达式将条件应用于tasks数组的每个元素,就像它是顶级文档一样。