MongoDB ID - how to implement sequential custom IDs?

Problem

Using Mongoose.

  • Mongo automatically creates an _id for each new document in a collection
  • I am creating a user system where i want to implement a simple, sequential id
  • Meaning the first user will be 1, the second 2, etc
  • This is working OK in development, I use the id to work with my user data throughout the app
  • I set a simple variable in my Node application that increases by one every time a user is saved
  • HOWEVER, when the server re-starts, the variable resets to the original value, this is a problem

Solution?

  • At this point in development it is not a big deal, but in the long term, my system is not going to hold up in production
  • I am a newb but I would imagine my Node server will not run uninterrupted forever

Are there any easy solutions to implement what I want?

  • I was thinking maybe i could save a separate document on my DB that contains my counter variable, and find it and increment it and save it again every time i save a user… this doesn’t seem like the most efficient thing in the world
  • my other idea is just to use the mongo _id to do all my lookups
  • idk i find the super long strings annoying and it would be nice to just see user id’s in a simple numerical order

Maybe someone with more db experience can chime in with their thoughts on my situation and ideas. Thanks.

When all else fails, read the documentation for creating sequential IDs.

Out of curiosity, why do you care if the id is 1,2, or 3 or a randomly created ID? Are you trying to use the id as a way of knowing the order in which the ids were created? If so, they use a date/time field for that.

good point. i read over the docs and determined i don’t need seq id’s in this case.

i have a created on value in my schema that generates Date.now when a user document is created.

i’m not sure why i cared to be honest. when i started this whole thing i went through this tutorial that was creating id’s in sequence and using those values as part of the routing. so for example, they would have a multiple channels and each channel would be found at /channel/:id and obviously, the integers in sequence look better than the long default _id. since, i am not setting up my routes like that, i guess i don’t even need the sequential ids.

man, i newb out sometimes, there is so much to learn. thanks for responding.

1 Like

Those “annoying super long strings” are called ObjectIds and have some unique properties:

  • they include the creation timestamp of the document, which you can retrieve with getTimestamp
  • sorting by _id will (roughly*) sort by the creation time of the documents
  • ObjectIds are unique within your database, and likely unique in the universe
  • they’re fast to generate…
  • …and don’t require a round-trip to the database, which you correctly figured out would be necessary for your solution (“save a separate document on my DB that contains my counter variable”)

* orangematter.solarwinds .com/2019/12/22/what-is-mongodbs-id-field-and-how-to-use-it/