Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB atomic operation


May 17, 2021 MongoDB


Table of contents


MongoDB atomic operation

Mongodb does not support transactions, so be aware of this when applying it in your project. Whatever the design, don't ask mongodb to guarantee the integrity of your data.

But mongodb provides many atomic operations, such as document saving, modification, deletion, etc.

The so-called atomic operation is either this document saved to Mongodb, or not saved to Mongodb, there will be no query to the document did not save the complete situation.


Atomic operation data model

Consider the following examples, library books, and checkout information.

The example illustrates how to ensure that fields associated with atomic operations (update: update) are synchronized in 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 can be settled and update new billing information.

The available and checkout fields embedded in the same document ensure that they are updated synchronously:

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

Atomic operations are commonly used commands

$set

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

{ $set : { field : value } }

$unset

Used to remove a key.

{ $unset : { field : 1} }

$inc

$inc you can increase or subtract a key of a document that has a value of a numeric type (only for numbers that meet the requirements).

{ $inc : { field : value } }

$push

Usage:

{ $push : { field : value } }

Add the value to the field, the field must be an array type, if the field does not exist, an array type will be added.

$pushAll

With $push, you can append more than one value to an array field at a time.

{ $pushAll : { field : value_array } }

$pull

Remove a value equal to values equal to values from the array field.

{ $pull : { field : _value } }

$addToSet

Add a value to the array, and only if the value is not inside the array.

$pop

Delete the first or last element of the 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 } ] }