Search code examples
pythonmongodbpymongo

Pymongo: update document only if value of a field matches a provided value


Is it possible to update a document based on the condition that the value of a field in that document matches a value that I provide?

I can easily do this in two steps but was wondering if there was a way to do it in a single call to MongoAtlas.


Solution

  • It is a single step execution. You don't need two steps.

    Reference

    And Python implementation is same syntax as plain queries.

    db.collection.update_one({
      filter condition goes here
    },{
      '$set': {
        update fields goes here
      }
    }, upsert=False) //Options