Docs 菜单

ObjectId.toString()

在此页面上

ObjectId.toString()

string返回 的ObjectId() 表示形式。

可以使用 ObjectId.toString() 查找托管在以下环境中的部署:

如下示例:

  • 生成新的 ObjectId() 并将其存储在变量 myObjectId 中。

  • 使用 toString() 方法创建 myObjectId 的字符串表示。

  • 将字符串表示存储在变量 myObjectIdString 中。

mongosh 中运行以下命令:

myObjectId = ObjectId("507c7f79bcf86cd7994f6c0e")
myObjectIdString = myObjectId.toString()

该操作会返回以下字符串:

507c7f79bcf86cd7994f6c0e

要确认 myObjectIdString 的类型,请使用 typeof JavaScript 操作符:

typeof myObjectIdString

该操作返回以下内容:

string

在此页面上