YouTip LogoYouTip

Mongodb Atomic Operations

# MongoDB Atomic Operations MongoDB does not support transactions, so you need to be mindful of this when applying it in your projects. Regardless of the design, do not require MongoDB to guarantee data integrity. However, MongoDB provides many atomic operations, such as saving, modifying, and deleting documents, which are all atomic operations. An atomic operation means that either the document is saved to MongoDB, or it is not saved to MongoDB. There will not be a situation where a queried document is not completely saved. * * * ## Atomic Operation Data Model Consider the following example, which involves library books and checkout information. The example illustrates how to ensure that embedded fields within the same document are synchronized for atomic operations (update). 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 check if a book is available for checkout and update the new checkout information. The embedded `available` and `checkout` fields within the same document 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() } } }} ) * * * ## Common 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 be used to increment or decrement a numeric value for a key in a document (the value must be a valid number). { $inc : { field : value } } #### $push Usage: { $push : { field : value } } Appends `value` to `field`. `field` must be an array type. If `field` does not exist, a new array type field is created and the value is added. #### $pushAll Similar to `$push`, but allows appending multiple values to an array field at once. { $pushAll : { field : value_array } } #### $pull Removes all elements from the array `field` that are equal to `value`. { $pull : { field : _value } } #### $addToSet Adds a value to an array, but only if the value is not already present in the array. #### $pop Removes the first or last element of an array. { $pop : { field : 1 } } #### $rename Renames a field. { $rename : { old_field_name : new_field_name } } #### $bit Performs bitwise operations on integer values. {$bit : { field : {and : 5}}} #### Positional 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 } ] }
← Mongodb Advanced IndexingMongodb Analyzing Queries β†’