Database Manual / Reference / mongosh Methods / Object Constructors

ObjectId() (mongosh method方法)

Description描述

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.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Returns a new ObjectId. The 12-byte ObjectId consists of:返回一个新的ObjectId。12字节的ObjectId由以下部分组成:

  • A 4-byte timestamp, representing the ObjectId's creation, measured in seconds since the Unix epoch.一个4字节的时间戳,表示ObjectId的创建,以Unix纪元以来的秒为单位。
  • 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.每个客户端进程生成一次5字节的随机值。此随机值对于机器和过程是唯一的。如果流程重新启动或流程的主节点发生更改,则会重新生成此值。
  • A 3-byte incrementing counter per client-side process, initialized to a random value. The counter resets when a process restarts.每个客户端进程有一个3字节的递增计数器,初始化为随机值。当进程重新启动时,计数器会重置。

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).对于时间戳和计数器值,最重要的字节在字节序列中首先出现(大端序)。这与其他BSON值不同,在其他值中,最低有效字节首先出现(小字节序)。

If an integer value is used to create an ObjectId, the integer replaces the timestamp.如果使用整数值创建ObjectId,则该整数将替换时间戳。

Compatibility兼容性

You can use ObjectId() for deployments hosted in the following environments:您可以将ObjectId()用于在以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

ObjectId() can accept one of the following inputs:可以接受以下输入之一:

Input Type输入类型Description描述
hexadecimalOptional. 可选。A 24 character hexadecimal string value for the new ObjectId.新ObjectId的24个字符的十六进制字符串值。
integerOptional. 可选。The integer value, in seconds, is added to the Unix epoch to create the new timestamp.以秒为单位的整数值被添加到Unix纪元中以创建新的时间戳。

Methods方法

ObjectId() has the following methods:有以下方法:

Methods方法Description描述
ObjectId.getTimestamp()Returns the timestamp portion of the object as a Date.将对象的时间戳部分作为Date返回。
ObjectId.toString()Returns the ObjectId as a hexadecimal string.以十六进制字符串形式返回ObjectId。

Behavior行为

Starting in MongoDB 5.0, mongosh replaces the legacy mongo shell. 从MongoDB 5.0开始,mongosh取代了传统的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.ObjectId()方法在mongosh中的工作方式与传统mongo shell不同。有关遗留方法的更多信息,请参阅遗留mongoShell

Examples示例

Generate a New ObjectId生成新的对象ID

To generate a new ObjectId, use ObjectId() with no argument:要生成新的ObjectId,请使用不带参数的ObjectId()

newObjectId = ObjectId()

In this example, the value of newObjectId is:在此示例中,newObjectId的值为:

ObjectId("507f1f77bcf86cd799439011")

Return a Hexadecimal String返回十六进制字符串

To return the ObjectId as a hexadecimal string, use the toString() method.要将ObjectId作为十六进制字符串返回,请使用toString()方法。

ObjectId("507f191e810c19729de860ea").toString()

The method returns:该方法返回:

507f191e810c19729de860ea

Specify a Date指定日期

You can use a custom Date to specify an ObjectId.您可以使用自定义Date指定对象ID。

1

Set a variable for your specified date为您指定的日期设置一个变量

Internally, Date objects are stored as signed 64-bit integer that represents the number of milliseconds since the Unix epoch. 在内部,Date对象存储为有符号的64位整数,表示自Unix纪元以来的毫秒数。To learn more, see Date().要了解更多信息,请参阅Date()

myDate = new Date( "2024-01-01" )
2

Convert your Date object to seconds将Date对象转换为秒

timestamp = Math.floor( myDate / 1000 )
3

Set your new ObjectId with timestamp as the argument使用timestamp作为参数设置新的ObjectId

You can verify the Date by using ObjectId.getTimestamp().您可以使用ObjectId.getTimestamp()来验证日期。

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.如果要调整ObjectId时间戳,请使用整数生成新的ObjectId。

newObjectId = ObjectId(32)

The ObjectId value resembles:ObjectId值类似于:

ObjectId("00000020f51bb4362eee2a4d")

The example ObjectId consists of:示例ObjectId由以下部分组成:

  • 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.ObjectId的前四个字节是自Unix纪元以来的秒数。在这个例子中,ObjectId时间戳是00000020,十六进制为32

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():如果要使用十六进制字符串指定ObjectId,请在调用ObjectId()时传递一个唯一的24个字符的十六进制值:

newObjectId = ObjectId("507f191e810c19729de860ea")