Home > Back-end >  Auto Increment field in NodeJS and MongoDB
Auto Increment field in NodeJS and MongoDB

Time:01-04

I am working on NodeJS

While inserting the data into MongoDB database with the help of NodeJS and Mongoose, I am getting the object Id which is more then 24-digit long.

I want to provide id like 1, 2, 3 4....which is nothing a auto increment digit.

Kindly help me to solve this puzzle.

CodePudding user response:

By default MongoDB does not support auto-increment sequence as a default feature like some relational databases.

But you can implement using Triggers. I found this official guide: https://www.mongodb.com/basics/mongodb-auto-increment

CodePudding user response:

Find the below solutions with mongoose-sequence.

This plugin lets you create fields which autoincrement their value:

every time a new document is inserted in a collection or when you explicitly want to increment them This increment can be:

global: every document has a unique value for the sequence field scoped: the counter depends on the value of other field(s) Multiple counters can be set for a collection.

  •  Tags:  
  • Related