I'm trying to delete the following dict in this array:
{
"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
is a sub-document in this record.
I try to delete with this:
db.projects.update({'ProjectID': "8ToGTAUjT+CbcH68ZYuW8Q=="},
{'$pull': {'Tasks.TaskID': "JxHddpQNSguzOqg1sdsdKtyQ=="}})
This however does not delete this sub-document. How can this dict in the array be deleted?
To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator. Now field "UserZipCode": "20010" has been removed from a doubly-nested array.
Try to use this code:
db.projects.update(
{'ProjectID': "8ToGTAUjT+CbcH68ZYuW8Q=="},
{'$pull': {Tasks:{ TaskID: "JxHddpQNSguzOqg1sdsdKtyQ=="}}}
)
The $pull
expression applies the condition to each element of the tasks array as though it were a top-level document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With