Transaction management in MongoDB - How to achieve like ACID transactions in RDBMS

You are here : Home / MongoDB Tutorial


Contents of page >


1) MongoDb does not support transactions on multiple documents?
Yes, MongoDB does not support transactions on multiple document.
But, in MongoDB you can perform atomic operations on a single document.
2) With lack of transactions on multiple documents, Can mongoDB achieve ACID transactions?
Using atomic operations on a single document we can meet all the  requirements of ACID transactions in RDBMS.
3) Example of transaction management in MongoDB>
MongoDB allows you to store data in embedded document form.
So, you can store all your data in form of embedded documents (nested documents or nested arrays) in one document and update the whole document atomically in single operation.
But, in case of RDBMS you will need multiple tables to represent such data and their ACID transactions to come into picture.
4) Tips to design database in MongoDB which will meet all the requirements of ACID transaction in RDBMS >
As we discussed that MongoDB does not support transactions on multiple document.But, in MongoDB you can perform atomic operations on a single document.
So, while designing your database and collections you must try and ensure that all the related data (as much as possible) which is needed to be updated atomically must be placed in single document as embedded documents (in form of nest arrays OR nested documents) so you can take advantage of MongoDB support for performing atomic operations on a single document.
5) Example of how to design collections in MongoDB to >
  • Take advantage of MongoDB support for performing atomic operations on a single document. And
  • Meet all the requirements of ACID transaction in RDBMS


6) Example of Database collections where multiple transactions may create discrepancies in data >


As we discussed that MongoDB does not support transactions on multiple document. But, in MongoDB you can perform atomic operations on a single document.
Here, we will Create separate STUDENT and PHONE collections.
And, documents in phone contain a reference to the student document.
So, If one transaction update STUDENT documents and other transaction updating PHONE documents might create some discrepancies.
STEP 6.1) create and insert in STUDENT collection >
db.STUDENT.insert({
 "_id": 1,
 "FIRST_NAME": "Ankit"
})
db.STUDENT.insert({
 "_id": 2,
 "FIRST_NAME": "Sam"
})

STEP 6.2) create and insert in PHONE collection >
db.PHONE.insert({
 "_id": 11,
 "PHONE_NUMBER": 1234,
 "STUDENT_ID":  {
     "$ref": "STUDENT",
     "$id": 1,
     "$db": "mydb"
  }
})
db.PHONE.insert({
 "_id": 12,
 "PHONE_NUMBER": 2345,
 "STUDENT_ID":  {
     "$ref": "STUDENT",
     "$id": 1,
     "$db": "mydb"
  }
})
db.PHONE.insert({
 "_id": 13,
 "PHONE_NUMBER": 3456,
 "STUDENT_ID":  {
     "$ref": "STUDENT",
     "$id": 2,
     "$db": "mydb"
  }
})
db.PHONE.insert({
 "_id": 14,
 "PHONE_NUMBER": 4567,
 "STUDENT_ID":  {
     "$ref": "STUDENT",
     "$id": 2,
     "$db": "mydb"
  }
})

7) Example of Database collections where multiple transactions will NOT create any discrepancies in data >


Let’s rephrase above example for better transaction management.
Create STUDENT collections.
Embed the PHONE documents (completely) in the STUDENT document.
So, now multiple transaction cannot create any discrepancies as STUDENT contains PHONE documents in embedded form and in MongoDB you can perform atomic operations on a single document.
>create and insert in STUDENT collection >
db.STUDENT.insert({
 "_id": 1,
 "FIRST_NAME": "Ankit",
 "PHONE": [
   {"PHONE_NUMBER": 1234},
   {"PHONE_NUMBER": 2345}
 ]
})
db.STUDENT.insert({
 "_id": 2,
 "FIRST_NAME": "Sam",
 "PHONE": [
   {"PHONE_NUMBER": 3456},
   {"PHONE_NUMBER": 4567}
 ]
})


8) Summary -
MongoDB does not support transactions on multiple document. But, in MongoDB you can perform atomic operations on a single document.


Using atomic operations on a single document we can meet all the  requirements of ACID transactions in RDBMS.
Example of transaction management in MongoDB>
MongoDB allows you to store data in embedded document form.
So, you can store all your data in form of embedded documents (nested documents or nested arrays) in one document and update the whole document atomically in single operation.
Tips to design database in MongoDB which will meet all the requirements of ACID transaction in RDBMS >
While designing your database and collections you must try and ensure that all the related data (as much as possible) which is needed to be updated atomically must be placed in single document as embedded documents (in form of nest arrays OR nested documents) so you can take advantage of MongoDB support for performing atomic operations on a single document.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>

What is MongoDB - A quick introduction to database



Difference in MongoDB and RDBMS - What are Database, collection, document and field in MongoDB

Delete documents in collection (table) in mongoDB

How to Delete all documents from collection in MongoDB

Install and connect to MongoDB on windows

Create new database in mongoDB

See list of all databases in MongoDb, switch database

Create new collection(table) in mongoDB


Labels: MongoDB
eEdit
Must read for you :