Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB Atomic Operations

MongoDB Atomic Operations

mongodb does not support transactions, so when applying it in your project, be aware of this. Regardless of the design, do not require mongodb to guarantee data integrity.

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

The so-called atomic operation means that either the document is saved to Mongodb or it is not saved to Mongodb; there will be no situation where a queried document is not saved completely.


Atomic operation data model

Consider the following example: library books and checkout information.

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



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 is available for checkout and 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() } }

           }

} )


Commonly used atomic operation commands

$set

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

{ $set : { field : value } }

$unset

Used to delete a key.

{ $unset : { field : 1} }

$inc

$inc can increment or decrement a key whose value is numeric (only numbers that meet the requirement) in a document.

{ $inc : { field : value } }

$push

Usage:



{ $push : { field : value } }

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

$pushAll

Same as $push, but multiple values can be appended to an array field at once.

{ $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, and only add it when the value is not already in the array.

$pop

Remove the first or last element of the array

{ $pop : { field : 1 } }

$rename

Modify the field name

{ $rename : { old_field_name : new_field_name } }

$bit

Bitwise 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 } ] }