AutoIncrement using mongoose

Please Guys what is the best way to implement AutoIncrement using mongoose, i tried the best two option and it wasnt working mongoose-sequence mongoose-auto-increment
const AutoIncrement = require('mongoose-sequence')(mongoose)

interface userTracks extends IUserTrack, mongoose.Document {}

const userModel = new mongoose.Schema(
{
fullName: {
type: String,
unique: false,
lowercase: true
},
email: {
type: String,
unique: true,
lowercase: true
},
phone: {
type: String
},
country: {
type: String,
unique: false,
lowercase: true
}
},
{ timestamps: true }
)

userModel.plugin(AutoIncrement, { inc_field: 'ticketNo', start_seq: 1000 })

export default mongoose.model<userTracks>('users', userModel)
const AutoIncrement = require('mongoose-sequence')(mongoose)

interface userTracks extends IUserTrack, mongoose.Document {}

const userModel = new mongoose.Schema(
{
fullName: {
type: String,
unique: false,
lowercase: true
},
email: {
type: String,
unique: true,
lowercase: true
},
phone: {
type: String
},
country: {
type: String,
unique: false,
lowercase: true
}
},
{ timestamps: true }
)

userModel.plugin(AutoIncrement, { inc_field: 'ticketNo', start_seq: 1000 })

export default mongoose.model<userTracks>('users', userModel)
3 Replies
Electronic
Electronic11mo ago
Stack Overflow
Mongoose auto increment
According to this mongodb article it is possible to auto increment a field and I would like the use the counters collection way. The problem with that example is that I don't have thousands of peo...
Leke
Leke11mo ago
Aint working
Electronic
Electronic11mo ago
I'm not too good with mongoose, but I'm guessing you could use the "pre" save hook Create a schema that keeps track of what you want to auto increment And perform a db call to it increment it and save the new document