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

Reset to specific value or decrement method #79

Closed
rip32700 opened this issue Dec 11, 2019 · 6 comments
Closed

Reset to specific value or decrement method #79

rip32700 opened this issue Dec 11, 2019 · 6 comments
Labels

Comments

@rip32700
Copy link

Sometimes a model object that I use the incrementer for gets deleted. Once that happens, I would like to decrement the counter again or to reset it to the value before.

Would this feature be possible to add? Otherwise you have to directly manipulate the collection as a workaround.

@ramiel
Copy link
Owner

ramiel commented Dec 11, 2019

What if the document that gets deleted is not the last one. i.e. if the last document inserted has the counter to 10 and you remove the document 5 what should happen? You cannot reset the counter to 9 or 4. This library mimic the SQL index where the index is not really bound to the entries in the DB but is more like an internal counter.

I would say this is not a feature I'd implement but maybe I'm missing some use case, so let's discuss further if you think it's worth it.

@ravirajawasthi
Copy link

Maybe a function like resetCounter, but to change the value of counter to a specific value?

I new to mongoose and your package is super useful.

@ramiel
Copy link
Owner

ramiel commented Mar 4, 2020

What is the use case? I always have the feeling that the use case may be wrong, so let me understand better yours

@dandv
Copy link
Contributor

dandv commented Apr 13, 2020

I can imagine a use case of deleting the last batch of documents, e.g. a bunch of spam users or spam comments. But it's got to be the very last, and I'm not sure re-setting the counter is worth the risk that you'll later overwrite a legit document.

There are already risks with not using the native ObjectID.

@ramiel
Copy link
Owner

ramiel commented Apr 14, 2020

But even in that case, you should not reset the counter or decrease. When you delete documents, the counter must be always the highest possible, not the first available. In SQL world it's how it works. If you delete last row which has an index of 5, next insertion will have index of 6, not 5.

@vtrolinux
Copy link

Product.counterReset('idProduct', function(err) {
// Now the counter is 0
});

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

No branches or pull requests

5 participants