3.35. MongoDB atomic operation

发布时间 :2025-10-25 12:32:59 UTC      

Mongodb does not support transactions, so be aware of this when applying it to your project. No matter what the design, do not require mongodb to ensure the integrity of the data.

But mongodb provides many atomic operations, such as saving, modifying, deleting, etc., all of which are atomic operations.

The so-called atomic operation is that either the document is saved to Mongodb, or it is not saved to Mongodb, and the queried document will not be saved completely.

3.35.1. Atomic operation data model

Consider the following example, library books and checkout information.

An example shows how to ensure that embedded fields associated with atomic operations (update: updates) 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 it. db.collection.findAndModify() Method to determine whether the book can be settled and update the new settlement information.

Embedded in the same document available And checkout Fields to ensure that 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() } }
           }
} )

3.35.2. Common commands for atomic operation

$set

Used to specify a key and update the key value, if the key does not exist and create.

{ $set : { field : value } }

$unset

Used to delete a key.

{ $unset : { field : 1} }

$inc

Inc can add or subtract a key whose value is numeric (only a number that meets the requirements) of the document.

{ $inc : { field : value } }

$push

Usage:

{ $push : { field : value } }

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

$pushAll

Same as $push, except that you can append multiple values to an array field at a time.

{ $pushAll : { field : value_array } }

$pull

Removes an equal value from the array field.

{ $pull : { field : _value } }

$addToSet

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

$pop

Delete the first or last element of the array

{ $pop : { field : 1 } }

$rename

Modify field name

{ $rename : { old_field_name : new_field_name } }

$bit

Bit operation, integer type

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

偏移操作符

> 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 } ] }
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.