ObjectId.toString()
On this page
ObjectId.toString()-
Returns the string representation of the返回ObjectId().ObjectId()的字符串表示形式。
Example实例
The following example:以下示例:
-
Generates a new生成一个新的ObjectId()and stores it in the variablemyObjectId.ObjectId()并将其存储在变量myObjectId中。 -
Creates a string representation of使用myObjectIdusing 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