Description描述
ObjectId(<value>)-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. 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:可以接受以下输入之一:
hexadecimal | |
integer |
Methods方法
ObjectId() has the following methods:有以下方法:
ObjectId.getTimestamp() | |
ObjectId.toString() |
Behavior行为
Starting in MongoDB 5.0, 从MongoDB 5.0开始,mongosh replaces the legacy mongo shell. 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,请使用不带参数的ObjectId() with no argument: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 要将ObjectId作为十六进制字符串返回,请使用toString() method.toString()方法。
ObjectId("507f191e810c19729de860ea").toString()
The method returns:该方法返回:
507f191e810c19729de860eaSpecify a Date指定日期
You can use a custom Date to specify an ObjectId.您可以使用自定义Date指定对象ID。
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" )Set your new ObjectId with timestamp as the argument使用timestamp作为参数设置新的ObjectId
timestamp as the argumentYou 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,一个四字节的时间戳,00000020A five byte random element,一个五字节的随机元素,f51bb4362eA 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 ObjectId的前四个字节是自Unix纪元以来的秒数。在这个例子中,ObjectId时间戳是00000020 which is 32 in hexadecimal.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")