Node.js MongoDB ObjectId to string

mongodb, mongoose, node.js

Solution

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());

Problem

IN Node.js, with MongoDB, Mongoosejs as orm I am doing this I have a model, User ``` User.findOne({username:'someusername'}).exec(function(err,user){ console.log(user) //this gives full object with something like {_id:234234dfdfg,username:'someusername'} //but console.log(user._id) //give undefined. }) ``` Why? And how to get the _id to string then?

Original source