This plugin create fields which autoincrement their value every time a new document is inserted in a collection.
yarn add mongoose-counters
npm i mongoose-counters
This plugin accept a series of options.
- id: Id of the counter. Is mandatory only for scoped counters but its use is strongly encouraged.
- incField: The name of the field to increment. Mandatory, default is
_id
- referenceFields: The field to reference for a scoped counter. Optional
- collectionName: The collection name to mantain the status of the counters. Mandatory, default is
counters
Use as you would any Mongoose plugin:
import * as mongoose from 'mongoose';
import mongooseCounter from 'mongoose-counters';
const counter = mongooseCounter(mongoose);
const schema = new mongoose.Schema({ ... });
schema.plugin(counter, { ...options });
const model = model('MyModel', schema);
The increment can be:
global
: every document has a unique value for the counter fieldscoped
: the counter depends on the value of other field(s)
Let's say you want to have an id
field in your collection
which has an unique auto-incremented value.
The model schema is something like this:
ModelSchema = mongoose.Schema({
myAttr: String
...
});
mongoose.model('ModelName', ModelSchema);
You don't need to define the id
field in your schema because the plugin automatically set it for you. The only thing you have to do is to call:
ModelSchema.plugin(counter, { incField: 'id' });
Every time a new model entry is created, the id
field will have an incremental number.
If you want to increment the _id
field which is special to mongoose, you have to explicitly specify it as a Number and tell mongoose to not interfer:
ModelSchema = mongoose.Schema({
_id: Number,
myAttr: String
}, { _id: false });
ModelSchema.plugin(AutoIncrement);
In this case you don't have to specify incField
because the default value is _id
Let say our users are organized for country
and city
. And we want to save the inhabitant_number
according to the two informations.
The schema is like this:
UserSchema = mongoose.Schema({
name: String,
country: String,
city: String,
inhabitant_number: Number
});
Every time a new Parisian is added the counting of Parisians have to be incremented. The inhabitants of New York must not interfer and have their separated counting. We should define a scoped counter which increment the counter depending on the value of other fields.
UserSchema.plugin(AutoIncrement, {id: 'inhabitant_seq', inc_field: 'inhabitant_number', reference_fields: ['country','city'] });
Notice that we have to use an id for our sequence, otherwise the plugin will raise an error.
It's possible to programmatically reset a counter through the Model static method counterReset(id, reference, callback)
. The method take those parameters:
- id: the counter to reset. It's mandatory
- reference: Let you reset only a specific reference of the counter, if the counter has referenced fields. Optional. By default it reset all the counters for the
id
- callback: A callback which receive an error in case of any. Mandatory
Model.counterReset('the_counter_id', (err) => { ... });
Model.counterReset('the_counter_id', { ref_field_1: 'ref_value_1', ref_field_2: 'ref_value_2'}, (err) => { ... });
This plugin is inspired by ramiel/mongoose-sequence.