Convert ObjectID (Mongodb) to String in JavaScript

vhlen picture vhlen · May 10, 2013 · Viewed 77.7k times · Source

I want to convert ObjectID (Mongodb) to String in JavaScript. When I get a Object form MongoDB. it like as a object has: timestamp, second, inc, machine. I can't convert to string.

Answer

anubiskong picture anubiskong · Nov 4, 2013

Try this:

objectId.str

See the doc.

ObjectId() has the following attribute and methods:

[...]

  • str - Returns the hexadecimal string representation of the object.