You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
7.4.1
Node.js version
16.18.1
MongoDB server version
4.2
Typescript version (if applicable)
4.7.3
Description
I'm encountering a similar issue as described in #8852, while using the mongoose built-in transaction wrapper Connection#transaction.
To simplify the problem, I've created the reproducible code below.
The problem arises when I attempt to save a new document within a transaction. If a TransientTransactionError occurs during the first try and triggers a retry, the doc's $isNew flag is not reset before the second attempt. Consequently, the subsequent save operation throws a DocumentNotFound error because it attempts to send an update operation behind the scenes instead of an insert.
Upon digging into the code, I discovered that the state reset, which should occur here, only takes place after two failed tries (in the case of a retry), and not before each retry attempt.
Prerequisites
Mongoose version
7.4.1
Node.js version
16.18.1
MongoDB server version
4.2
Typescript version (if applicable)
4.7.3
Description
I'm encountering a similar issue as described in #8852, while using the
mongoose
built-in transaction wrapperConnection#transaction
.To simplify the problem, I've created the reproducible code below.
The problem arises when I attempt to save a new document within a transaction. If a
TransientTransactionError
occurs during the first try and triggers a retry, the doc's $isNew flag is not reset before the second attempt. Consequently, the subsequent save operation throws aDocumentNotFound
error because it attempts to send an update operation behind the scenes instead of an insert.Upon digging into the code, I discovered that the state reset, which should occur here, only takes place after two failed tries (in the case of a retry), and not before each retry attempt.
Steps to Reproduce
Expected Behavior
I expected that the state should be reset before every retry attempt, which should prevent the
DocumentNotFound
error from occurring.At the moment, my workaround is moving
const doc = new Model({ a: "b" });
into the transaction block.The text was updated successfully, but these errors were encountered: