Concept of Atomic Operations in MongoDB

Last updated on Jun 19 2021
Anudhati Reddy

Table of Contents

Concept of Atomic Operations in MongoDB

Anudhati Reddy

Arundhati Reddy is a technology expert and she is currently working as a No-SQL Specialist. He has expertise in No-SQL Databases particularly MongoDB etc.

Let us understand the concept of MongoDB Atomic operations.

MongoDB – Atomic Operations

MongoDB doesn’t support multi-document atomic transactions. However, it does provide atomic operations on a one document. So, if a document has hundred fields the update statement will either update all the fields or none, hence maintaining atomicity at the document-level.

Model Data for Atomic Operations

The recommended approach to take care of atomicity would be to stay all the related information, which is usually updated together during a one document using embedded documents. This would make sure that all the updates for a one document are atomic.

Consider the subsequent merchandises document −

{

“_id”:1,

“merchandise_name”: “Samsung S3”,

“category”: “mobiles”,

“merchandise_total”: 5,

“merchandise_available”: 3,

“merchandise_bought_by”: [

{

“customer”: “john”,

“date”: “7-Jan-2014”

},

{

“customer”: “mark”,

“date”: “8-Jan-2014″

}

]

}

In this document, we’ve embedded the knowledge of the customer who buys the merchandise within the merchandise_bought_by field. Now, whenever a replacement customer buys the merchandise, we’ll first check if the merchandise is still available using merchandise_available field. If available, we’ll reduce the value of merchandise_available field as well as insert the new customer’s embedded document in the merchandise_bought_by field. We’ll use findAndModify command for this functionality because it searches and updates the document in the same go.

>db.merchandises.findAndModify({

query:{_id:2,merchandise_available:{$gt:0}},

update:{

$inc:{merchandise_available:-1},

$push:{merchandise_bought_by:{customer:”rob”,date:”9-Jan-2014″}}

}

})

Our approach of embedded document and using findAndModify query makes sure that the merchandise purchase information is updated only if it the merchandise is available. And the whole of this transaction being in the same query, is atomic.

In contrast to this, consider the scenario where we may have kept the merchandise availability and the information on who has bought the merchandise, separately. In this case, we will first check if the merchandise is available using the primary query. Then within the second query we will update the purchase information. However, it is possible that between the executions of these two queries, some other user has purchased the merchandise and it is no more available. Without knowing this, our second query will update the purchase information based on the result of our first query. This will make the database inconsistent because we’ve sold a merchandise which isn’t available.

So, this brings us to the end of blog. This Tecklearn ‘Concept of Atomic Operations in MongoDB’ helps you with commonly asked questions if you are looking out for a job in MongoDB and No-SQL Database Domain.

If you wish to learn and build a career in MongoDB or No-SQL Database domain, then check out our interactive, MongoDB Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/mongodb-training/

MongoDB Training

About the Course

Tecklearn’s MongoDB Training helps you to master the NoSQL database. The course makes you job-ready by letting you comprehend schema design, data modelling, replication, and query with MongoDB through real-time examples. Along with this, you’ll also gain hands-on expertise in installing, configuring, and maintaining the MongoDB environment, including monitoring and operational strategies from this online MongoDB training. Upon completion of this online training, you will hold a solid understanding and hands-on experience with MongoDB.

Why Should you take MongoDB Training?

  • MongoDB – a $36 billion to a $40 billion market growing at 8% to 9% annually – Forbes.com
  • Average salary of a Mongo DB certified professional is $134k – Indeed.com
  • MongoDB has more than 900 customers, including 27 Fortune 100 companies like Cisco, eBay, eHarmony, MetLife & Salesforce.com

What you will Learn in this Course?

Introduction to MongoDB and Importance of NoSQL

  • Understanding the basic concepts of RDBMS
  • What is NoSQL Database and its significance
  • Challenges of RDBMS and How NoSQL suits Big Data needs
  • Types of NoSQL Database and NoSQL vs. SQL Comparison
  • CAP Theorem and Implementing NoSQL
  • Introduction to MongoDB and its advantages
  • Design Goals for MongoDB Server and Database, MongoDB tools
  • Collection, Documents and Key Value Pair
  • Introduction to JSON and BSON documents
  • MongoDB installation

MongoDB Installation

  • MongoDB Installation
  • Basic MongoDB commands and operations,
  • Mongo Chef (MongoGUI) Installation

Schema Design and Data Modelling

  • Why Data Modelling?
  • Data Modelling Approach
  • Data Modelling Concepts
  • Difference between MongoDB and RDBMS modelling
  • Challenges for Data Modelling
  • Model Relationships between Documents
  • Data Model Examples and Patterns
  • Model Tree Structures

CRUD Operations

  • MongoDB Architecture
  • CRUD Introduction and MongoDB CRUD Concepts
  • MongoDB CRUD Concerns (Read and Write Operations)
  • Cursor Query Optimizations and Query Behaviour in MongoDB
  • Distributed Read and Write Queries
  • MongoDB Datatypes

Indexing and Aggregation Framework

  • Concepts of Data aggregation and types and data indexing concepts
  • Introduction to Aggregation
  • Approach to Aggregation
  • Types of Aggregation: Pipeline, MapReduce and Single Purpose
  • Performance Tuning

MongoDB Administration

  • Administration concepts in MongoDB
  • MongoDB Administration activities: Health check, recovery, backup, database sharing and profiling, performance tuning etc.
  • Backup and Recovery Methods for MongoDB
  • Export and Import of Data from MongoDB
  • Run time configuration of MongoDB

MongoDB Security

  • Security Introduction
  • MongoDB security Concepts and security approach
  • MongoDB integration with Java and Robomongo

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Concept of Atomic Operations in MongoDB"

Leave a Message

Your email address will not be published. Required fields are marked *