3.35. MongoDB atomic operation

发布时间 : 2025-10-25 13:34:22 UTC      

Page Views: 10 views

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 } ] } 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。