Modeling data for atomic operations
MongoDB is relaxing many of the typical Atomicity, Consistency, Isolation, and Durability (ACID) constraints found in RDBMS. The default operation mode does not support transactions, making it important to keep the state consistent across operations, especially in the event of failures.
Some operations are atomic at the document operation level:
update()
findandmodify()
remove()
These are all atomic (all-or-nothing) for a single document.
This means that, if we embed information in the same document, we can make sure they are always in sync.
An example would be an inventory application, with a document per item in our inventory. Every time a product is placed in a user’s shopping cart, we decrement the available_now
value by one and append the userid
value to the shopping_cart_by
array.
With total_available = 5
, available_now = 3
, and shopping_cart_count = 2
, this use case could look like the following: