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);});