Anand
Anand

Reputation: 21310

Transaction support in MongoDB

I am new to MongoDB. I read that MongoDB does not support multi-document transactionshere http://docs.mongodb.org/manual/faq/fundamentals/.

If I want to save data in two collections(A and B) atomically, then i can't do that using MongoDB i.e. if save fails in case of B, still A will have the data. Isn't it a big disadvantage?

Still, people are using MongoDB rather than RDBMS. Why?

Upvotes: 19

Views: 19749

Answers (8)

Isabel Jinson
Isabel Jinson

Reputation: 8661

UPDATE MongoDB have already started to support multi-document transactions. https://docs.mongodb.com/manual/core/transactions/

MongoDB does not support multi-document transactions.

However, MongoDB does provide atomic operations on a single document. Often these document-level atomic operations are sufficient to solve problems that would require ACID transactions in a relational database.

For example, in MongoDB, you can embed related data in nested arrays or nested documents within a single document and update the entire document in a single atomic operation. Relational databases might represent the same kind of data with multiple tables and rows, which would require transaction support to update the data atomically.

Upvotes: 10

Mithilesh Jha
Mithilesh Jha

Reputation: 175

MongoDB 4.0 adds support for multi-document ACID transactions now. For reference See Refrence

Upvotes: 15

Pranab Sharma
Pranab Sharma

Reputation: 739

Starting from version 4.0 MongoDB will add support for multi-document transactions. So you will have the power of the document model with ACID guarantees in MongoDB. Transactions in MongoDB will be like transactions in relational databases.

For details visit this link: https://www.mongodb.com/blog/post/multi-document-transactions-in-mongodb?jmp=community

Upvotes: 0

e-oj
e-oj

Reputation: 207

This question is quite old but for anyone who stumbles upon this page, you could use fawn. It's an npm package that solves this exact problem. Disclosure: I wrote it

Say you have two bank accounts, one belongs to John Smith and the other belongs to Broke Individual. You would like to transfer $20 from John Smith to Broke Individual. Assuming all first name and last name pairs are unique, this might look like:

var Fawn = require("fawn");
var task = Fawn.Task()

//assuming "Accounts" is the Accounts collection 
task.update("Accounts", {firstName: "John", lastName: "Smith"}, {$inc: {balance: -20}})
  .update("Accounts", {firstName: "Broke", lastName: "Individual"}, {$inc: {balance: 20}})
  .run()
  .then(function(){
    //update is complete 
  })
  .catch(function(err){
    // Everything has been rolled back. 

    //log the error which caused the failure 
    console.log(err);
  });

Caveat: tasks are currently not isolated(working on that) so, technically, it's possible for two tasks to retrieve and edit the same document just because that's how MongoDB works.

It's really just a generic implementation of the two phase commit example on the tutorial site: https://docs.mongodb.com/manual/tutorial/perform-two-phase-commits/

Upvotes: 1

ThienSuBS
ThienSuBS

Reputation: 1622

Only support Single Document Transaction.

You can see it at: https://docs.mongodb.com/v3.2/tutorial/perform-two-phase-commits/

Upvotes: -1

keshav
keshav

Reputation: 754

MongoDB does not support transactions as in Relational DB. ACID postulates in transactions is a complete different functionality provided by storage engines in MySQL

Some of the features of InnoDB engine in MySQL:

  • Crash Recovery
  • Double write buffer
  • Auto commit settings
  • Isolation Level

This is what MongoDB community has to say:

MongoDB does not have support for traditional locking or complex transactions with rollback.

MongoDB aims to be lightweight, fast, and predictable in its performance. By keeping transaction support extremely simple, MongoDB can provide greater performance especially for partitioned or replicated systems with a number of database server processes.

The purpose of a transaction is to make sure that the whole database stays consistent while multiple operations take place.

But in contrary to most relational databases, MongoDB isn't designed to run on a single host. It is designed to be set up as a cluster of multiple shards where each shard is a replica-sets of multiple servers (optionally at different geographical locations).

But if you are still looking for way to make transactions possible:

  • Try using document level atomicity provided by mongo
  • two phase commit in Mongo provides simple transaction mechanism for basic operations
  • mongomvcc is built on the top of mongo and also supports transaction as they say
  • Hybrid of MySQL and Mongo

Upvotes: 3

Md Rahatur Rahman
Md Rahatur Rahman

Reputation: 3244

Multi-document updates or “multi-document transactions” using a two-phase commit approac described here: http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/

Upvotes: 1

stalk
stalk

Reputation: 12054

MongoDB doesn't support transactions, but saving one document is atomic.

So, it is better to design you database schema in such a way, that all the data needed to be saved atomically will be placed in one document.

Upvotes: 3

Related Questions