ObjectId.toString()
On this page本页内容
ObjectId.toString()
-
Returns the string representation of the
ObjectId()
.
Example实例
The following example:以下示例:
Generates a new生成一个新的ObjectId()
and stores it in the variablemyObjectId
.ObjectId()
并将其存储在变量myObjectId
中。Creates a string representation of使用myObjectId
using thetoString()
method.toString()
方法创建myObjectId
的字符串表示形式。Stores the string representation in the variable将字符串表示形式存储在变量myObjectIdString
.myObjectIdString
中。
Run the following commands in 在mongosh
:mongosh
中运行以下命令:
myObjectId = ObjectId("507c7f79bcf86cd7994f6c0e")
myObjectIdString = myObjectId.toString()
The operation returns the following string:该操作返回以下字符串:
507c7f79bcf86cd7994f6c0e
To confirm the type of 要确认myObjectIdString
, use the typeof
JavaScript operator:myObjectIdString
的类型,请使用typeof
JavaScript运算符:
typeof myObjectIdString
The operation returns the following:该操作返回以下内容:
string