I have a document from a mongoose find that I want to extend before JSON encoding and sending out as a response. If I try adding properties to the doc it is ignored. The properties don't appear in Object.getOwnPropertyNames(doc)
making a normal extend not possible. The strange thing is that JSON.parse(JSON.encode(doc))
works and returns an object with all of the correct properties. Is there a better way to do this?
The lean() function tells mongoose to not hydrate query results. In other words, the results of your queries will be the same plain JavaScript objects that you would get from using the Node. js MongoDB driver directly, with none of the mongoose magic.
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.
Mongoose documents represent a one-to-one mapping to documents as stored in MongoDB. Each document is an instance of its Model. Documents vs Models. Retrieving. Updating Using save()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With