Search code examples
pythonmongodbpymongo

MongoDB (pymongo). Replace all blank strings with null


I'm trying to figure a quick way to replace blank strings ('') that I have in a lot of my documents (across many fields) with MongoDB's null value.

In MongoDB docs, I was hoping to use ReplaceAll, but I can't figure out how to execute the same thing in pymongo unless I do replace_one().

Is there an efficient way to do this?


Solution

  • Currently pymongo does not support replace all. Most effective way to update many documents would be update_many. It looks something like this:

    db.my_db.update_many(
        filter={'field_1': ""},
        update={'$set': {'field_1': None}}
    )