I have a mongodb database and I am using mongoose with nodejs. When I console a mongoose object, I get the usual keys that I have defined in my schema.
console.log(mongooseObject);
/*
{
_id: 5b8b7d627defb42e3f6349ef,
name: 'John Doe',
}
*/
But when I tried to console it's keys using Object.keys(), it prints 5 keys that are not really the part of my schema and must being used internally by mongoose for various operations.
console.log(Object.keys(mongooseObject)) // [ '$__', 'isNew', 'errors', '_doc', '$init' ]
My question is, how does console.log determines what part of the object to be printed? When just logging the object directly, how does it work its way through these keys and their values to determine what to print?
Is there a toString() like function that a class defines which describes the JSON representation of an object? Something which is then used by console.log to actually print.
Edit: I'm getting a lot of mongoose related solutions suggessting me to use .toJSON() and .toObject(). Perhaps I should have made it clear that I'm not really looking for mongoose related stuff. I just used it's example because that's how I notice such peculiar behaviour. I'm just generally intereted in how javascript is working behind here. And can I write a Class where I can describe how an object being instanciated from it should be consoled?