- #128
Common _id ObjectId Issues You Will Encounter When Using MongoDB
In MongoDB, every document has an
_idfield that serves as the primary key. If it is not explicitly assigned, MongoDB will automatically generate one. In practice, I’ve run into several questions related to this design:- Does
_idexpose sensitive information? - Can we create our own custom IDs?
- Why is the
_iddata type an ObjectId instead of a string or a number? What’s the reason for this special design?
According to the official ObjectId documentation, an ObjectId is a 12-byte value composed of the following parts:
- A 4-byte timestamp, representing the ObjectId’s creation time, measured in seconds since the Unix epoch.
- A 5-byte random value, generated once per client process. This value is unique to the machine and process. It is regenerated if the process restarts or if the primary node changes.
- A 3-byte incrementing counter, initialized to a random value for each client process. The counter resets when the process restarts.
The design goals of ObjectId are:
- Guaranteed uniqueness: Ensures uniqueness across different services and machines.
- Embedded timestamp: Records the document’s creation time.
- Efficiency: Designed for distributed systems, allowing IDs to be generated without coordinating with other services.
Back to the original questions:
-
Does it contain sensitive information? ObjectId does include the document’s creation time. In certain business scenarios, this may be considered sensitive, and it can also be sampled to roughly estimate data volume.
-
Can
_idbe customized? Yes. However, a common practice is to let_idhandle technical uniqueness, while introducing a separate field for semantic meaning.{_id: ObjectId("..."),userId: "u_123456"} -
What are the advantages of ObjectId?
- Globally unique and can be generated on the client side without requesting from the database, making it ideal for distributed environments.
- Enforced by MongoDB as the document’s primary key, with a unique index created automatically.
- Compact in size and sortable (due to the embedded timestamp), making it more friendly for indexing and storage efficiency.
- Does
- #127
- #126
- #125
- #124
- #123
- #122
- #121
- #120
- #119
- #118
- #117
- #116
- #115
- #114
- #113
- #112
- #111
- #110
- #109
- #108
- #107
- #106
- #105
- #104
- #103
- #102
- #101
- #100
- #99
- #98
- #97
- #96
- #95
- #94
- #93
- #92
- #91
- #90
- #89
- #88
- #87
- #86
- #85
- #84
- #83
- #82
- #81
- #80
- #79
- #78
- #77
- #76
- #75
- #74
- #73
- #72
- #71
- #70
- #69
- #68
- #67
- #66
- #65
- #64
- #63
- #62
- #61
- #60
- #59
- #58
- #57
- #56
- #55
- #54
- #53
- #52
- #51
- #50
- #49
- #48
- #47
- #46
- #45
- #44
- #43
- #42
- #41
- #40
- #39
- #38
- #37
- #36
- #35
- #34
- #33
- #32
- #31
- #30
- #29
- #28
- #27
- #26
- #25
- #24
- #23
- #22
- #21
- #20
- #19
- #18
- #17
- #16
- #15
- #14
- #13
- #12
- #11
- #10
- #9
- #8
- #7
- #6
- #5
- #4
- #3
- #2
- #1