简体   繁体   中英

Mongodb Pymongo remove fields from documents

My idea is to remove certain data updated earlier on certain conditions. For that I cant update documents with any null/None value. That should not going to happen in Mongodb. As a workaround, I am got to use $unset in MonngoDB. The docs says, here that we can pop a field from a document using $unset . like this

db.collections.update({ id: uid }, { $unset: [ "<field1>", "<field2>", ... ] })

My question is this. How can we achieve this with Pymongo.?

Pymongo is generally similar to the mongodb shell commands, but it has to work within the constraints of the python syntax. Importantly, you must put the $ operators in quotes, so "$unset" as per below.

db.collections.update_one({ "id": uid }, { "$unset": [ "", "", ... ] })

If this doesn't work, post the error you are getting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM