简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM