How do you turn a Mongoose document into a plain object? How do you turn a Mongoose document into a plain object? mongoose mongoose

How do you turn a Mongoose document into a plain object?


Mongoose Models inherit from Documents, which have a toObject() method. I believe what you're looking for should be the result of doc.toObject().

http://mongoosejs.com/docs/api.html#document_Document-toObject


Another way to do this is to tell Mongoose that all you need is a plain JavaScript version of the returned doc by using lean() in the query chain. That way Mongoose skips the step of creating the full model instance and you directly get a doc you can modify:

MyModel.findOne().lean().exec(function(err, doc) {    doc.addedProperty = 'foobar';    res.json(doc);});


the fast way if the property is not in the model :

document.set( key,value, { strict: false });


matomo