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

Increate the count only when creation success. #99

Open
cwang1221 opened this issue Feb 7, 2021 · 1 comment
Open

Increate the count only when creation success. #99

cwang1221 opened this issue Feb 7, 2021 · 1 comment

Comments

@cwang1221
Copy link

Dear Experts,

I found the count is increased no matter if the creation of the document success or not. E.g.

  1. I create a new user, the count will be 1
  2. I try to create the second user, but since I give a duplicate key to it, the creation failed (err.name === "MongoError" && err.code === 11000)
  3. I create a new user, the count is 3, but actually it should be 2.

Any idea on this? Thank you so much!

@ramiel
Copy link
Owner

ramiel commented Feb 7, 2021

Yes the hook used is a pre-save hook, so the counter is incremented before the document is actually saved. This is safer than a post hook and better mimic the SQL behavior. At the moment there's no option to use a post hook but you can use a workaround. You can disable hooks completely and run manual increment by yourself. See the disable_hooks options and the manual increment documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants