MongoDB Atomic Operations

MongoDB does not support transactions, so you need to pay attention to this when applying it in your project. No matter what design you use, do not require MongoDB to guarantee data integrity.

However, MongoDB provides many atomic operations, such as document saving, modification, deletion, etc., all of which are atomic operations.

An atomic operation means that either the document is saved to MongoDB or it is not saved to MongoDB; there will never be a situation where a queried document is not completely saved.


Atomic Operations Data Model

Consider the following example of library books and checkout information.

The example illustrates how to ensure that fields associated with atomic operations (update: update) of embedded fields in the same document are synchronized.

book = {
          _id: 123456789,
          title: "MongoDB: The Definitive Guide",
          author: [ "Kristina Chodorow", "Mike Dirolf" ],
          published_date: ISODate("2010-09-24"),
          pages: 216,
          language: "English",
          publisher_id: "oreilly",
          available: 3,
          checkout: [ { by: "joe", date: ISODate("2012-10-15") } ]
        }

You can use the db.collection.findAndModify() method to determine whether a book can be checked out and to update the new checkout information.

Embed the available and checkout fields in the same document to ensure these fields are updated synchronously:

db.books.findAndModify ( {
   query: {
            _id: 123456789,
            available: { $gt: 0 }
          },
   update: {
             $inc: { available: -1 },
             $push: { checkout: { by: "abc", date: new Date() } }
           }
} )

Common Commands for Atomic Operations

$set

Used to specify a key and update its key value; if the key does not exist, it will be created.

{ $set : { field : value } }

$unset

Used to delete a key.

{ $unset : { field : 1} }

$inc

$inc can perform increment/decrement operations on a key whose value is numeric (only numbers that meet the requirements) in a document.

{ $inc : { field : value } }

$push

Usage:

{ $push : { field : value } }

Append value to field. The field must be of array type. If the field does not exist, a new array type will be added.

$pushAll

Same as $push, except that multiple values can be appended to an array field at one time.

{ $pushAll : { field : value_array } }

$pull

Remove a value equal to value from the array field.

{ $pull : { field : _value } }

$addToSet

Add a value to the array, but only add it when the value is not already in the array.

$pop

Remove the first or last element of an array.

{ $pop : { field : 1 } }

$rename

Modify the field name.

{ $rename : { old_field_name : new_field_name } }

$bit

Bit operation, integer type.

{$bit : { field : {and : 5}}}

Offset operator.

> t.find() { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }
 
> t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )
 
> t.find() { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }
Other Extensions