0

I am creating an API to get and post values from a Database using the MEAN stack.

I want to automatically generate the userID starting from 0 when each entry is created in the usesrschema.

var userSchema = new mongoose.Schema({
    //userId: Number,
    username: String,
    password: String, //hash created from password
    created_at: {type: Date, default: Date.now}
})

How is it that I add a userID starting from 0 and autoincrementing with each insertion?

1 Answers1

1

You have a bunch of choices in the NPM to make life easier on yourself.

mongodb-autoincrement

mongoose-auto-increment - specifically for using with Mongoose

The list can go on and on, (as there are quite a few libraries to accomplish this), my suggestion would be to try the mongoose-auto-increment which is pretty much what you are looking for.

twg
  • 1,075
  • 7
  • 11