Search code examples
c#mongodbmongodb-.net-driver

MongoDb c# driver update multiple documents in one atomic operation


I want to ask you if the following code will be executed in one atomic operation. I'm using mongodb c# driver.

The input to the method is list of id of the objects I want to update.

public void Update(IEnumerable<string> ids)
{
    var query = Query<T>.Where(t => ids.Contains(t.Id));
    var update = Update.Set("Modified", DateTime.Now); //this is just example of update

    var options = new MongoUpdateOptions {Flags = UpdateFlags.Multi};

    Collection.Update(query, update, options);
}

I'm interesting about the case, when I have milions of documents to update. What will happen if there will be a fail (power or hardware problem) during this update? Will be the database in a consistent state?

thanks.


Solution

  • MongoDB do not support transaction or atomic multi documents. MongoDB perform atomic operation only on one document.

    You can check this in the documentation of Mongodb

    So if you update with your query 1000 documents and your server crash during this operation, some documents may be updated, other won't.