-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Labels
enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new featureThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone
Description
In mongoose 5.7.3, seems there is no session
option (or any option) on Model.remove
API.
So how do I remove a document with condition in a transaction in mongoose?
Though I can do find a document first, then call Model.prototype.remove
. But it would becomes 2 query to make, the problem is, document may be mutate between the 2 queries, which cause data inconsistency.
For example
0. Do something in transaction
- Query the
doc
in transaction with{ a: 1 }
- (somebody else update the document outside of transaction to
{a: 2}
) - call
doc.remove()
- Do something in transaction
- Commit transaction
If I'm correct, the transaction would succeed without error and the document will be deleted, which is unwanted.
If there we can use session in Model.remove
, we can just call Model.remove(condition, {session})
and verify the nRemoved
to get rid of the unwanted behavior.
Metadata
Metadata
Assignees
Labels
enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new featureThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature