mongoose-sequence icon indicating copy to clipboard operation
mongoose-sequence copied to clipboard

Possible to set the value manually for certain documents?

Open rip32700 opened this issue 3 years ago • 1 comments

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?

rip32700 avatar Jul 10 '20 07:07 rip32700

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) => {
    ...
  });
} 

ramiel avatar Jul 13 '20 09:07 ramiel