Reputation: 180
I'm trying to delete a piece of data under a id if you don't know what I mean just yet, I will explain in detail, I've seen tons of other documents on remove
or delete
but most of them contain deleting the id and all it's data completely, or it just mentions a bulk delete; basically anything that falls under a category is deleted affecting other documents.
Say we have collection peoples
{"_id": 1, "cool": 1}
{"_id": 2, "cool": 1}
{"_id": 3, "cool": 2}
{"_id": 4, "cool": 1}
{"_id": 5, "cool": 3}
#dont judge my sense of randomness
Now if I have a requirement in which if cool
is equal to 4, then delete cool, if this was True then the file would look something like this;
Added +1 cool to every file
{"_id": 1, "cool": 2}
{"_id": 2, "cool": 2}
{"_id": 3, "cool": 3}
{"_id": 4, "cool": 2}
{"_id": 5}
As you can see every id
with the data cool
has been increased by 1, and since we made the requirement of if cool is equal to 4, then we should delete the data cool
under id : 5
(not the file + id completely!!!).
So my question is; how exactly would I do this, specify a certain id and then delete a certain piece of data under that id without affecting other files nor deleting the file and the id itself?
Thank you for reading and your time! :)))
Upvotes: 1
Views: 652
Reputation: 75
So first you'd need to get the _id
of the document.
If you don't know how to find this, you can make a variable for all of the documents in the collection and iterate through it.
all_documents = collection.find({})
for document in all_documents:
if document['cool'] == 4:
collection.update({"_id": document["_id"]},{ $unset: {"cool": ""}})
Essentially, what you were asking for is this line:
collection.update({"_id": document["_id"]},{ $unset: {"cool": ""}})
You can read about it here
Upvotes: 1