mongosh
method. This is not
the documentation for a language-specific driver, such as Node.js.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
Returns a new ObjectId consists of:
A 4-byte timestamp, representing the ObjectId's creation, measured in seconds since the Unix epoch.
A 5-byte random value generated once per client-side process. This random value is unique to the machine and process. If the process restarts or the primary node of the process changes, this value is re-generated.
A 3-byte incrementing counter per client-side process, initialized to a random value. The counter resets when a process restarts.
For timestamp and counter values, the most significant bytes appear first in the byte sequence (big-endian). This is unlike other BSON values, where the least significant bytes appear first (little-endian).
If an integer value is used to create an ObjectId, the integer replaces the timestamp.
Compatibility
You can use ObjectId()
for deployments hosted in the following
environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
ObjectId()
can accept one of the following inputs:
Input Type | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
| Optional. A 24 character hexadecimal string value for the new ObjectId. | ||||||||||
| Optional. The integer value, in seconds, is added to the
ExamplesGenerate a New ObjectIdTo generate a new ObjectId, use
In this example, the value of
Return a Hexadecimal StringTo return the ObjectId as a hexadecimal string, use the
The method returns:
Specify a DateYou can use a custom Date to specify an ObjectId. 1 Set a variable for your specified dateInternally, Date objects are stored as signed
64-bit integer that represents the number of milliseconds since the
Unix epoch. To learn more, see
3 Set your new ObjectId with |
newObjectId = ObjectId(timestamp) |
ObjectId("6592008029c8c3e4dc76256c")
Specify an Integer String
If you want to adjust the ObjectId timestamp, use an integer to generate a new ObjectId.
newObjectId = ObjectId(32)
The ObjectId value resembles:
ObjectId("00000020f51bb4362eee2a4d")
The example ObjectId consists of:
A four byte time stamp,
00000020
A five byte random element,
f51bb4362e
A three byte counter,
ee2a4d
The first four bytes of the ObjectId are the number of seconds since the
Unix epoch. In this example, the ObjectId timestamp is
00000020
which is 32
in hexadecimal.
Specify a Hexadecimal String
If you want to use a hexadecimal string to specify an ObjectId, pass a
unique, 24 character hexadecimal value when you call
ObjectId()
:
newObjectId = ObjectId("507f191e810c19729de860ea")
Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!
Check out: eBank.nz (Art Generator) |
Netwrck.com (AI Tools) |
Text-Generator.io (AI API) |
BitBank.nz (Crypto AI) |
ReadingTime (Kids Reading) |
RewordGame |
BigMultiplayerChess |
WebFiddle |
How.nz |
Helix AI Assistant