- Reference >
mongo
Shell Methods >- Object Constructors and Methods >
- ObjectId.toString()
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
. - Creates a string representation of
myObjectId
using thetoString()
method. - Stores the string representation in the variable
myObjectIdString
.
Run the following commands in mongosh
:
The operation returns the following string:
To confirm the type of myObjectIdString
, use the typeof
JavaScript operator:
The operation returns the following: