nodejs mongodb object id to string

Solution 1:

Try this:

user._id.toString()

A MongoDB ObjectId is a 12-byte UUID can be used as a HEX string representation with 24 chars in length. You need to convert it to string to show it in console using console.log.

So, you have to do this:

console.log(user._id.toString());

Solution 2:

Take the underscore out and try again:

console.log(user.id)

Also, the value returned from id is already a string, as you can see here.