M
M
magary42015-07-07 11:15:43
MongoDB
magary4, 2015-07-07 11:15:43

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

4 answer(s)
S
Sergey, 2015-07-07
@begemot_sun

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.

K
Konstantin Kitmanov, 2015-07-07
@k12th

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.

B
bromzh, 2015-07-08
@bromzh

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.

L
lega, 2015-07-08
@lega

Does it make sense to have incremental IDs for all tables in mongodb?

Usually not.
The standard _id (ObjectID) has the advantage that it is created before being written to the database, i.e. in order to find out the final id, you do not need to wait for the database entry to complete, unlike the incremental id.
This allows, for example, to give the client the result immediately, without waiting for the record (when you need to give the id). Or the "client" can set the _id in the request itself, and not wait for a response, knowing what _id the document will have. This gives additional flexibility and performance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question