Mongoose.js transactions

前端 未结 6 1010
栀梦
栀梦 2021-01-30 18:14

I know MongoDB doesn\'t support transactions as relational databases do, but I still wonder how to achieve atomicity for several operations. Hunting around the web, I see people

相关标签:
6条回答
  • 2021-01-30 18:19

    Transactions are not supported in MongoDB out of the box. However they are implementable with Two-phase commit protocol. Here you can see official recommendation on how to do 2PCs in MongoDB. This approach is quite universal and can be applied to different scenarios such as

    • documents from different collections (your case)
    • more than 2 documents
    • custom actions on documents
    0 讨论(0)
  • 2021-01-30 18:21

    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/

    0 讨论(0)
  • 2021-01-30 18:26

    If you really must have transactions across multiple documents types (in separate collections) the means to achieve this is with a single table that stores actions to take.

    db.actions.insert(
    { actions: [{collection: 'players', _id: 'p1', update: {$set : {name : 'bob'} } },
                {collection: 'stories', _id: 's1', update: {$set : {location: 'library'} } }], completed: false }, callback);
    

    This insert is atomic, and all done at once. You then can perform the commands in the 'actions' collection and mark them as complete or delete them as you complete them, calling your original callback when they are all completed. This only works if your actions processing loop is the only thing updating the db. Of course you'll have to stop using mongoose, but the sooner you do that the better you'll be anyway.

    0 讨论(0)
  • 2021-01-30 18:34

    Transactions are now supported in Mongoose >= 5.2.0 and MongoDB >= 4.0.0 (with replica sets)

    https://mongoosejs.com/docs/transactions.html

    0 讨论(0)
  • 2021-01-30 18:38

    There have been a few attempts to implement solutions. None of them are particularly active at the time of writing. But maybe they work just fine!

    • https://github.com/anand-io/mongoose-transaction Lacks documentation, 1 year idle.

    • https://github.com/niahmiah/mongoose-transact Documented, 2 years idle.

    • https://github.com/rain1017/memdb Offers transaction support but actually does a lot more than just that, 6 months idle.

    niahmiah's README is worth looking at. It notes some disadvantages of using transactions, namely:

    • All updates and removals for the relevant collections should go through the transaction processor, even when you weren't intentionally doing a transaction. If you don't do this, then transactions may not do what they are supposed to do.
    • Those updates, as well as the transactions you initially wanted, will be significantly slower than normal writes.

    niahmiah also suggests that there may be alternatives to using transactions.


    Just a tip: If you don't like the idea of locking up your frequently used collections, you could consider separating the sensitive (transactional) data from the less sensitive data.

    For example, if you are currently holding credits in the User collection, then you would be better off separating it into two collections: User and UserWallet, and moving the credits field into the wallets. Then you can freely update User documents without fear of interfering with transactions on the UserWallet collections.

    0 讨论(0)
  • 2021-01-30 18:40

    You can simulate a transaction by manually rolling-back the changes whenever an error occurs. In your above example, simply remove the saved document if the other one fails.

    You can do this easily using Async:

        function rollback (doc, cb) {
          doc.remove(cb);
        }
    
        async.parallel([
              player.save.bind(player),
              story.save.bind(story),
          ],
          function (err, results) {
            if (err) {
              async.each(results, rollback, function () {
                console.log('Rollback done.');
              });
            } else {
              console.log('Done.');
            }
          });
    

    Obviously, the rollback itself could fail -- if this is a not acceptable, you probably need to restructure your data or choose a different database.

    Note: I discussed this in detail on this post.

    0 讨论(0)
提交回复
热议问题