MongoDB: Why is it not good to modify the objectid of “_id” to a normal increment ID

  mongodb, question

I have studied sql before, and now I am studying mongodb. Seeing objectID’s various IDs that want to change it to auto increment starting from 1 like sql, I gave it all to update. I heard that it is not possible to do this, or to keep mongo’s original automatically generated obectID, but why?

In fact, it is easy to get the answer by analyzing the principle above. Suppose you have a stack of numbers, 1 to 100, below:

  1. A man came and asked you for a number and you gave him one. That’s no problem.

  2. When they arrived together, they both asked you for a number. What would you do? Slow down and come one by one. Send one after the other. Slow down but make do with it.

  3. At the same time, 10 people came to ask you what to do with the number, and 100 people?

Imagine the number of people coming as concurrent requests, and it is not difficult to find that the rapid allocation of numbers has become a bottleneck. Therefore, self-increasing ID is not good for RDBMS either. But fortunately, the traditional database is stand-alone, which is nothing more than adding a lock to deal with the competition in the internal memory. The problem is not big. MongoDB is a distributed database. Several machines need to coordinate with each other. You take 1, I take 2, he takes 3. This lock needs to be coordinated through the network, which is very inefficient.
Considering the purpose of distribution, one of them is to improve concurrency, so self-increasing ID and high concurrency are actually running counter to each other. To ensure correct increment in a distributed environment will inevitably affect efficiency. Besides, the self-increasing ID does not have much advantage except that it looks refreshing, so it must be abandoned. (Remember that ObjectID can actually be sorted, that is, inserted in chronological order)