简体   繁体   中英

Why MongoDB does not use the same type of IDs as other DBMS?

Before learning MongoDB, I have some experience with MySQL, PostgreSQL. Actually, when I used them, I created many tables and I used id as a Primary Key. The structure of the ID like AUTO_INCREMENT INTEGER and it was much easier when having some queries. But MongoDB uses some big, random string for that. What is the reason for that? Is it any security issue, or something else?

MongoDB is designed as a distributed and highly scalable system.

With a sequential integer used as a document identifier, the identifier must be generated by the database. This causes three issues:

  1. The database must have a central service that generates the identifiers (so that it only uses each identifier once). In MongoDB you can set up a sharded cluster; the shards are independent and there is no single node that is responsible for all of the data.

  2. Because all operations must hit this identifier generation service, it becomes a scalability bottleneck.

  3. You must retrieve the identifier from the database to have the complete document.

MongoDB instead encourages identifier generation to be done by the clients (drivers or applications). The drivers generate identifiers that are very likely to be globally unique without having to coordinate with other application instances or any of the server nodes. This means:

  • Identifier generation is parallelizable and does not need to be done by any single node, process or service.
  • Identifier generation does not create contention in the database.
  • A client is able to generate the complete document and does not need to receive a part of it back from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM