ObjectId()
On this page本页内容
Description描述
ObjectId(<value>)
-
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 process. This random value is unique to the machine and process.每个进程生成一次的5字节随机值。该随机值对于机器和进程是唯一的。A 3-byte incrementing counter, initialized to a random value.一种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值不同,在BSON值中,最低有效字节首先出现(小端)。If an integer value is used to create an ObjectId, the integer replaces the timestamp.如果使用整数值创建ObjectId,则该整数将替换时间戳。ObjectId()
can accept one of the following inputs:可以接受以下输入之一:Input Type输入类型Description描述hexadecimal
Optional.可选的。A 24 character hexadecimal string value for the new ObjectId.新ObjectId的24个字符的十六进制字符串值。integer
Optional.可选的。The integer value, in seconds, is added to the Unix epoch以秒为单位的整数值被添加到Unix epochto create the new timestamp.
中以创建新的时间戳。
Methods方法
ObjectId()
has the following methods:具有以下方法:
ObjectId.getTimestamp() | |
ObjectId.toString() | |
ObjectId.valueOf() | ObjectId.self .ObjectId.self 。 |
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. ObjectId()
方法在mongosh
中的工作方式与在传统mongo
shell中的不同。For more information on the legacy methods, see Legacy mongo Shell.有关遗留方法的更多信息,请参阅传统mongo
Shell。
Examples实例
Generate a New ObjectId生成新的ObjectId
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:该方法返回:
507f191e810c19729de860ea
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 epochObjectId的前四个字节是自Unix epoch. In this example, the ObjectId timestamp is
00000020
which is 32
in hexadecimal.以来的秒数。在本例中,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")