Using $inc to increment a document property with Mongoose

I ran into another problem, which is kind of related to $inc.. So I'll post it here as it might help somebody else. I have the following code:

var Schema = require('models/schema.js');

var exports = module.exports = {};

exports.increase = function(id, key, amount, callback){
   Schema.findByIdAndUpdate(id, { $inc: { key: amount }}, function(err, data){
      //error handling
   }
}

from a different module I would call something like

var saver = require('./saver.js');

saver.increase('555f49f1f9e81ecaf14f4748', 'counter', 1, function(err,data){
    //error handling
}

However, this would not increase the desired counter. Apparently it is not allowed to directly pass the key into the update object. This has something to do with the syntax for string literals in object field names. The solution was to define the update object like this:

exports.increase = function(id, key, amount, callback){

   var update = {};
   update['$inc'] = {};
   update['$inc'][key] = amount;

   Schema.findByIdAndUpdate(id, update, function(err, data){
      //error handling
   }
}

Never used mongoose but quickly looking over the docs here it seems like this will work for you:

# create query conditions and update variables
var conditions = { },
    update = { $inc: { views: 1 }};

# update documents matching condition
Model.update(conditions, update).limit(limit).sort('date', -1).exec();

Cheers and good luck!