Docs HomeMongoDB Manual

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 variable myObjectId.生成一个新的ObjectId()并将其存储在变量myObjectId中。
  • Creates a string representation of myObjectId using the toString() 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