Home > OS >  How can mongoose automatically generate another set of Id?
How can mongoose automatically generate another set of Id?

Time:11-05

I am using mongoDB for storing data .I have create a user schema . The idea is ,apart from the mongodb automatic generate _id ,I will still need another automatic generate publish_id for showing to users(between:10-12) . The question is: how can I make it ? Thank you so much for your help !! enter image description here

CodePudding user response:

You can use nanoid. You have to install it with npm i nanoid. It will create you a random unique id.

In your controller when you are creating a new user, requires nanoid and then just use it. You can pass as parameter the length you want the id have. For exmaple: nanoid(48)

  • Related