Starting with version MongoDB 3.4, MongoDB no longer contains a machine identifier like the marked answer. But the id still contains a timestamp
that can be used to find out when the data was created. If you don't want the ID to store any information related to data that people can use to map data, you can follow what I do.
I have used MongoDB at the production stage. On my system, generally, all MongoDB documents have 2 unique IDs.
First: Default MongoDB _id
(Generated automatically by MongoDB)
I use this ID to query in the internal system. Examples such as those used for relations between collection
, caching, etc. Querying with ObjectId
is much faster than you querying other data types such as string
.
See https://stackoverflow.com/a/27897720/10861398
Second: Public ID (Generated by your application)
- Field:
pubId
- You can replace it with anything. (still make sure to use the relevant and concise name)
- Type: nanoid
- Small
- Fast (40% faster than
UUID
)
- Safe (Can be used in cluster)
- Compact (
A-Za-z0-9_-
)
- Portable (Nano ID was ported to 14 programming languages)
I use this as the main ID when the data is exported out and consumed by the client application. Examples such as data entry and exit between APIs
, integration with third-party applications, as id elements for rows in tables in client applications, etc.