user3336194
user3336194

Reputation: 89

Convert Mongoose object to particular json schema(object)

i have a requirement, when I am fetching data from MongoDB and using mongoose as an ORM. now the data that i fetch from DB, i keep in mongoose object. I do not want to send back all the information as response to client. I have a specific JSON response schema template. i want to convert the mongoose object to this particular JSON object and then send the response.

Upvotes: 1

Views: 383

Answers (1)

VtoCorleone
VtoCorleone

Reputation: 17203

Pass in a filter to your .find() method that only retrieves the properties that you want.

collection.find({}).select('name age');

If you have large datasets, you can go the opposite route and exclude the fields you don't want

collection.find({}).select('-created -createdBy');

http://mongoosejs.com/docs/queries.html

Upvotes: 1

Related Questions