Closed marcospgp closed 6 years ago
There is an interesting way this could be prevented:
__v
field incremented on every operation. This means if a transaction fails, we can include the previous __v
field in the rollback query, which will make it fail in case the document has been updated in the meantime.The question is, if this happens, what should we do? We can't go back anymore because our changes are now cemented into the history of the document we changed.
I suppose at this point it would be a better idea to just pick a database that supports transactions or wait for version 4 of MongoDB 😄
Edit: Another option is to instead of rolling back by changing the document to a previous version, directly rolling back the changes that were made, such as decrementing if previously and increment was done. This could also fail in some ways, for example if in the meantime a user's balance has come down to zero and now we wanted to decrement it in order to roll back a previous update.
Is it possible that between the time a document is retrieved and a transaction is attempted, the document has been updated by some other process?
This would mean if the transaction fails, the rollback will change the document back to an older version, losing the new data in the process.
Can this happen or is this prevented in some way?