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

Possible to set the value manually for certain documents? #88

Open
rip32700 opened this issue Jul 10, 2020 · 1 comment
Open

Possible to set the value manually for certain documents? #88

rip32700 opened this issue Jul 10, 2020 · 1 comment

Comments

@rip32700
Copy link

I would like to generally use the auto-incrementing value when creating new documents. However, if the user chooses to specify an explicit value, I want to use that value instead and also not increment the auto-inc counter. Is that possible without having to first save the document, then manually update and re-save the doc and manually decrement the counter collection again?

@ramiel
Copy link
Owner

ramiel commented Jul 13, 2020

hello @rip32700 I think you can follow what it explained in the Not automatic sequences section of the readme for your use case. Something like this

MySchema = mongoose.Schema({
    ...other props
    value: Number
});
// Do not use auto increment
MySchema.plugin(AutoIncrement, {id:'value_counter', inc_field: 'value', disable_hooks: true});

// Then, when saving
mydoc.save();
if(!mydoc.value) {
 mydoc.setNext('value_counter', (err,doc) => {
    ...
  });
} 

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

No branches or pull requests

2 participants