3

I understand that a multi update will not be atomic and there are chances that the update query may fail during the process. The error can be found via getLastError. But what if I want to roll back the updates that have already been done in that particular query? Any method simpler than the tedious two phase commits?

For instance, let me say I have a simple collection of some users and their phone models. Now I do a multi update on all the users who have a Nexus 4 to Nexus 5 (Dreamy, isn't it?). The only condition being, all or none - so all the N5s are taken back if even one N4 user doesn't get his. Now, somehow the mongodb fails in between, and I am stuck with a few users with N4 and a few with N5. Now, as I have gathered from the net is that I can't have mongo roll back directly. If the operation failed, I will have to do a manual update of the N5 users back to N4. And if that fails too somehow, keep repeating it. Or when I have a complicated collection, I will have to keep a new key viz. Status and update it with keywords updating/ updated.

This is what I understand. I wanted to know if there is any simpler way. I assume from the comments the answer is a big no.

Sudhanshu
  • 457
  • 5
  • 18
  • 4
    Yes, you'll have to reinvent transactions. – Sergio Tulentsev Mar 11 '14 at 13:13
  • 1
    MongoDB has no roll-back functionality. But when you describe what exactly you are trying to do and under what conditions it could fail in what way, we might be able to help you to find a work-around. – Philipp Mar 11 '14 at 13:16
  • 1
    It is a fair question I admit. There are no current tools in **node** that will help you. And Mongo transactions are right out. But perhaps your **real question** relates to how you have designed your schema. Which we don't see right now. Or otherwise understand what you are trying to do. Question edits are okay, unless your happy with the hypothetical responses this question will draw. – Neil Lunn Mar 11 '14 at 13:16
  • 1
    You likely have chosen the wrong database platform if you need to perform a transaction and roll-back as you describe it. There are a **LOT** of complex edge cases in a transaction system if you were to try to implement it on your own, especially if you need isolated transactions. I often think these scenarios are best handled by having a new document with a version that is made current AFTER everything necessary to indicate it is now current has completed. It means copied data, but often works. – WiredPrairie Mar 11 '14 at 14:31

0 Answers0