How do you turn a Mongoose document into a plain object?
the fast way if the property is not in the model :
document.set( key,value, { strict: false });
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);
});
Mongoose Model
s inherit from Document
s, 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