Answer the question
In order to leave comments, you need to log in
Does it make sense to have incremental IDs for all tables in mongodb?
For example, there is an orders table. by default, the id field is written as XBFFPcy5K7miHQ3gB. as a person who has worked with mysql for many years, I want to create a table couters and write human-understandable ids. do you need to do it
Answer the question
In order to leave comments, you need to log in
From a machine standpoint, no.
your MySQL ID must be unique, but it doesn't have to be sequential.
MongoDB also. As far as I understand, this is a HASH. Which is also unique to a first approximation.
You can always refer to this entry by this HASH.
Sometimes you want to immediately understand which record was added later, which one earlier. But, most likely, you will already have fields of type creationDate, updateDate.
Numbers are needed for two things: sorting and arithmetic. Doing arithmetic with IDs is stupid, but Mongo will sort by itself.
And why these human-understandable id? Leave everything as it is, but create an index on the field, indicate in the index that it will be unique. You can make the field numeric or string (slug). You will have to implement autoincrements yourself. Therefore, it is better to use what is. Or switch to postgresql. In the new version, the json type has appeared, there are sequences there, and indexes for a freaking fast search. Not for nothing that it was the postgres developer who filed a new engine for monga, which became much faster than the old one.
Does it make sense to have incremental IDs for all tables in mongodb?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question