Mongoose: what’s up with “_doc”?

_doc exist on the mongoose object.

Because mongooseModel.findOne returns the model itself, the model has structure (protected fields).
When you try to print the object with console.log it gives you only the data from the database, because console.log will print the object public fields.

If you try something like JSON.stringify then you get to see inside the mongoose model object. (_doc, state …)

In the case where you want to add more fields in the object and it’s not working

const car = model.findOne({_id:'1'})
car.someNewProp = true // this will not work

If later you set the property to the object car and you didn’t specify in the Model Schema before then Mongoose model is validating if this field exists and if it’s the valid type.
If the validation fails then the property will not be set.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)