Atomic Operation in MongoDB

MongoDB does not support Atomic Transaction over multiple Documents. However, it provides Atomic Operations on a single Document. Therefore, if a Document has hundreds of fields, the update command will either update all those fields or not update any fields, thus maintaining Atomicity at the Document level.

Data modeling for Atomic Transaction

The recommended method to maintain Atomicity will be to keep all relevant information, which is updated regularly in a single Document, using Embedded Documents . This ensures that all updates to a single Document are Atomic.

You follow the following products document :

 { "_id" : 1 , "product_name" : "Samsung S3" , "category" : "mobiles" , "product_total" : 5 , "product_available" : 3 , "product_bought_by" : [ { "customer" : "john" , "date" : "7-Jan-2014" }, { "customer" : "mark" , "date" : "8-Jan-2014" } ] } 

In this Document, we have embedded the information of the customer who purchased the product, into the product_bought_by field. Now, whenever a new customer purchases the product, we first check if the product is available using the product_available field. If available, we will reduce the value of the product_available field as well as insert the new client's Embedded Document into the product_bought_by field. We will use the findAndModify command for this feature because it searches and updates the Document at the same time.

 > db . products . findAndModify ({ query :{ _id : 2 , product_available :{ $gt : 0 }}, update :{ $inc :{ product_available :- 1 }, $push :{ product_bought_by :{ customer : "rob" , date : "9-Jan-2014" }} } }) 

The Embedded Document method and the query usage findAndModify ensures that product purchase information is only updated if the product is available. And all this transaction is Atomic.

Conversely, assuming a situation, we may have kept information about the availability of the product and information about who bought the product separately. In this situation, we first check the availability of the product by using the first query. Then, in the second query, we will update the purchase information. However, what may happen between the execution of the above two queries is that some users have purchased the product and it is no longer available. Without looking at this, our second query will update the purchase information based on the results of the first query. This creates conflicts in the database because we have sold a product that is not available.

According to Tutorialspoint

Previous article: Query analysis in MongoDB

Next lesson: Advanced indexing activity in MongoDB

4 ★ | 1 Vote

May be interested

  • Query Document in MongoDBQuery Document in MongoDB
    to query data from collection in mongodb, you need to use the find () method in mongodb.
  • Learn about security features and authentication in MongoDBLearn about security features and authentication in MongoDB
    in the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
  • ObjectId in MongoDBObjectId in MongoDB
    you have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
  • Text Search in MongoDBText Search in MongoDB
    starting with version 2.4, mongodb started supporting text indexes to search within the string content.
  • Shard in MongoDBShard in MongoDB
    sharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Close-up of the world's most accurate atomic clockClose-up of the world's most accurate atomic clock
    the team has been developing this clock for many years, with an accuracy that conventional atomic clocks using cesium atoms cannot achieve.
  • Advanced index operation in MongoDBAdvanced index operation in MongoDB
    create an index on the array ie create separate indexes for each of its fields. so in this situation, when we create an index on the array of tags, individual indexes will be created for their values: music, cricket and blogs.
  • Scientists have 'thrown' the atomic bomb into beer, soda and drank themScientists have 'thrown' the atomic bomb into beer, soda and drank them
    an atomic bomb explosion has the power to devastate everything around, tens of thousands of people must die immediately or become radioactive in the long run. but one thing is not ruined by the atomic bombs that are beer bottles, soda cans.