A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.mongodb.com/manual/reference/method/ObjectId/ below:

ObjectId() (mongosh method) - Database Manual

ObjectId(<value>)
Important mongosh Method

This page documents a 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. The 12-byte ObjectId consists of:

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.

You can use ObjectId() for deployments hosted in the following environments:

ObjectId() can accept one of the following inputs:

Input Type

Description

hexadecimal

Optional. A 24 character hexadecimal string value for the new ObjectId.

integer

Optional. The integer value, in seconds, is added to the Unix epoch to create the new timestamp.

ObjectId() has the following methods:

Starting in MongoDB 5.0, mongosh replaces the legacy mongo shell. The ObjectId() methods work differently in mongosh than in the legacy mongo shell. For more information on the legacy methods, see Legacy mongo Shell.

To generate a new ObjectId, use ObjectId() with no argument:

In this example, the value of newObjectId is:

ObjectId("507f1f77bcf86cd799439011")

To return the ObjectId as a hexadecimal string, use the toString() method.

ObjectId("507f191e810c19729de860ea").toString()

The method returns:

You can use a custom Date to specify an ObjectId.

Internally, Date objects are stored as signed 64-bit integer that represents the number of milliseconds since the Unix epoch . To learn more, see Date().

myDate = new Date( "2024-01-01" )
timestamp = Math.floor( myDate / 1000 )

You can verify the Date by using ObjectId.getTimestamp().

newObjectId = ObjectId(timestamp)
ObjectId("6592008029c8c3e4dc76256c")

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:

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.

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")

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4