• getTimestamp() 

Returns the timestamp portion of the object as a Date. 

• toString() 

Returns the JavaScript representation in the form of a string literal “ ObjectId(...) ”. 

Changed in version 2.2: In previous versions toString() returns the hexadecimal string representation, which as of version 2.2 can be retrieved by the str property. 

• valueOf() 

Returns the representation of the object as a hexadecimal string. The returned string is the str attribute. 

Changed in version 2.2: In previous versions, valueOf() returns the object. 

## Examples 

Consider the following uses ObjectId() class in the mongo shell: 

## Generate a new ObjectId 

To generate a new ObjectId, use the ObjectId() constructor with no argument: 

x = ObjectId() 

In this example, the value of x would be: 

ObjectId("507f1f77bcf86cd799439011") 

To generate a new ObjectId using the ObjectId() constructor with a unique hexadecimal string: 

y = ObjectId("507f191e810c19729de860ea") 

In this example, the value of y would be: 

ObjectId("507f191e810c19729de860ea") 

• To return the timestamp of an ObjectId() object, use the getTimestamp() method as follows: 

## Convert an ObjectId into a Timestamp 

To return the timestamp of an ObjectId() object, use the getTimestamp() method as follows: 

ObjectId("507f191e810c19729de860ea").getTimestamp() 

This operation will return the following Date object: 

ISODate("2012-10-17T20:46:22Z") 

## Convert ObjectIds into Strings 

Access the str attribute of an ObjectId() object, as follows: 

ObjectId("507f191e810c19729de860ea").str 

