mongoose field auto increment more easier
npm install mongoose-easy-auto-increment --save
import mongoose from 'mongoose';
import autoIncrement from 'mongoose-easy-auto-increment';
let connection = mongoose.createConnection('your-mongodb-url');
let userSchema = new mongoose.Schema({
name: String,
age: Number
});
userSchema.plugin(autoIncrement, { field: 'sn', collection: 'Counters' });
let User = connection.model('User', userSchema);
module.exports = {
User
};
name | description |
---|---|
field | The name of the field to increment. default is sn |
collection | Default collection name is SerialNumberCounters . You can change it. |