Model Data for Atomic Operations

Although MongoDB supports multi-document transactions for replica sets (starting in version 4.0) andsharded clusters (starting in version 4.2), for many scenarios, thedenormalized data model, as discussed on this page, will continue to beoptimal for your data and use cases.

Pattern

In MongoDB, a write operation on a single document is atomic. Forfields that must be updated together, embedding the fields within thesame document ensures that the fields can be updated atomically.

For example, consider a situation where you need to maintaininformation on books, including the number of copies available forcheckout as well as the current checkout information.

The available copies of the book and the checkout information should bein sync. As such, embedding the available field and thecheckout field within the same document ensures that you can updatethe two fields atomically.

  1. {
  2. _id: 123456789,
  3. title: "MongoDB: The Definitive Guide",
  4. author: [ "Kristina Chodorow", "Mike Dirolf" ],
  5. published_date: ISODate("2010-09-24"),
  6. pages: 216,
  7. language: "English",
  8. publisher_id: "oreilly",
  9. available: 3,
  10. checkout: [ { by: "joe", date: ISODate("2012-10-15") } ]
  11. }

Then to update with new checkout information, you can use thedb.collection.updateOne() method to atomically update boththe available field and the checkout field:

  1. db.books.updateOne (
  2. { _id: 123456789, available: { $gt: 0 } },
  3. {
  4. $inc: { available: -1 },
  5. $push: { checkout: { by: "abc", date: new Date() } }
  6. }
  7. )

The operation returns a document that contains information on thestatus of the operation:

  1. { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

The matchedCount field shows that 1 document matched the updatecondition, and modifiedCount shows that the operation updated 1document.

If no document matched the update condition, then matchedCount andmodifiedCount would be 0 and would indicate that you could notcheck out the book.