Docs Menu

Docs HomeMongoDB Manual

ObjectId.toString()

On this page

  • Example
ObjectId.toString()

Returns the string representation of the ObjectId().

The following example:

  • Generates a new ObjectId() and stores it in the variable myObjectId.

  • Creates a string representation of myObjectId using the toString() method.

  • Stores the string representation in the variable myObjectIdString.

Run the following commands in 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:

typeof myObjectIdString

The operation returns the following:

string
←  ObjectId.getTimestamp()ObjectId.valueOf() →

On this page

Share Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.