Mongoose: document.remove() removes document when session is aborted

Created on 26 Jan 2019  路  4Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?
Feature request

What is the current behavior?
document.save() does not save the document in the database if the transaction associated with the document is aborted( https://mongoosejs.com/docs/transactions.html#with-mongoose-documents-and-save- ) but document.remove() removes the document from the database when the transaction associated with the document is aborted

What is the expected behavior?
that document.remove() do not remove the document from the database if the transaction associated with the document is aborted

Please mention your node.js, mongoose and MongoDB version.
node v10.14.2, mongoose v5.4.6, mongodb v4.1.1

confirmed-bug

Most helpful comment

Fix will be in 5.4.9, turns out we weren't pulling the document's session for remove(), update(), and updateOne().

All 4 comments

Can you post a small example-snippet of code so we can easily reproduce this?
Also, if what you are describing is true, i think that would be a bug and not a feature request ;)

The most important feature of transactions is the ability to roll back ALL operations in the transaction using the abortTransaction() function. (https://mongoosejs.com/docs/transactions.html#aborting-a-transaction)

I'm experiencing the same here. When calling .save() nothing is persisted to the DB unless the transaction is committed, but when I call .remove() it'll remove the document regardless of the transaction having been committed or not.

So if I have a sequence of actions that are part of a transaction in a router, for instace:

1 save()
2 save()
3 remove()
4 save()
5 remove()
[an error gets thrown] <------
6 save()
7 commitTransaction()

And I throw an error in the middle of my router, changes 1, 2, 4 & 6 will not persist as the code never reached the commit point. But, for some reason, the remove() calls at 3 & 5 get persisted regardless, which kind of defeats the whole purpose of a transaction in the first place...

Fix will be in 5.4.9, turns out we weren't pulling the document's session for remove(), update(), and updateOne().

Was this page helpful?
0 / 5 - 0 ratings