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:

SomeModel.find({}, function (err, results) {
    results.forEach(function (instance, index, array) {
        array[index] = instance.toObject();
        array[index].foo = '<some method or value>';
Published on Sept. 11, 2012 by Nicolas and tagged mongoose. You can follow the discussion with the comment feed for this post. Feeling generous? Donate!

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
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.