Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete document in transaction #8211

Closed
HaoCherHong opened this issue Oct 3, 2019 · 0 comments
Closed

Delete document in transaction #8211

HaoCherHong opened this issue Oct 3, 2019 · 0 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@HaoCherHong
Copy link

HaoCherHong commented Oct 3, 2019

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

  1. Query the doc in transaction with { a: 1 }
  2. (somebody else update the document outside of transaction to {a: 2} )
  3. call doc.remove()
  4. Do something in transaction
  5. 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.

@vkarpov15 vkarpov15 added this to the 5.7.4 milestone Oct 7, 2019
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Oct 7, 2019
@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

2 participants