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! :)

Start a new thread

Cancel reply
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confusedMoneyHackerUpset confusedX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerNginxSilenceTorWorldInternet securityFont