Transaction support in MongoDB

2019-01-18 11:14发布

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?

8条回答
Root(大扎)
2楼-- · 2019-01-18 11:33

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/

查看更多
闹够了就滚
3楼-- · 2019-01-18 11:37

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/

查看更多
欢心
4楼-- · 2019-01-18 11:38

Only support Single Document Transaction.

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

查看更多
三岁会撩人
5楼-- · 2019-01-18 11:45

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
查看更多
我命由我不由天
6楼-- · 2019-01-18 11:46

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.

查看更多
劫难
7楼-- · 2019-01-18 11:49

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

查看更多
登录 后发表回答