Doing partial updates with Mongoose

I use Mongoose on top of mongodb native driver for Node.js. With Mongoose you can create ORM like objects in Javascript. However, Mongoose is in development stage, that is, not all functions documented are available, yet. One of these functions that does not seem to be ready is the Model::update() static function.

If you want to update part of a record in the database directly, without loading the whole document, the update function is very handy. Although update() function is not available as described in the documentation, with some workaround it works as follows:

var ObjectID = require('mongodb').ObjectID;
...
var recordid = ObjectID.createFromHexString(str_recordid);
UserGoal._collection.update({_id: recordid},
      {'$set': {key: value}}, function(err, data) { ... });
This entry was posted in sips. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>