Mongoose: Add data to returned result set Mongoose: Add data to returned result set mongoose mongoose

Mongoose: Add data to returned result set


Once you convert the resulting docs to plain JS objects by using lean(), they won't have any of the Mongoose model instance methods available to them like set, so you need to directly manipulate them using normal JavaScript object techniques:

searchQuery.lean().exec(function (err, authors) {   authors = authors.map(function(author) {       author.thumbnail = 'test';       return author;   });   res.json(authors);});

If you want to maintain the results as mongoose docs, then you need to pass {strict: false} as a third parameter to set to allow arbitrary fields to be added:

searchQuery.exec(function (err, authors) {   authors = authors.map(function(author) {       author.set('thumbnail', 'test', {strict: false});       return author;   });   res.json(authors);});


you can also just take the returned document in function(res,doc...)and do:

var docForMap = JSON.parse(JSON.stringify(doc));

This will give you a new copy that you can work with.