Modifying mongoose query objects

Mongoose is quite easy to use, but modifying or annotating a query result manually is a little tricky. The problem is that the result objects are mongoose documents that can't be modified at will.

In my case I was trying to change a Date type field to a human readable string. So here's a way to make the object editable:

Raw
SomeModel.find({}, function (err, results) {
    results.forEach(function (instance, index, array) {
        array[index] = instance.toObject();
        array[index].foo = '<some method or value>';
    });
});

1 comment

  1. avatar
    wrote this comment on
    It was my first meeting with an unwritable object in Javascript. Thank's a lot! :)

Reply

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo