Mongoose: Apply function on schema field in query condition

In MySQL it is possible to apply native function on a filed in where clause. For example.

SELECT * FROM t WHERE DATE(the_date) < '2012-11-19';

or

SELECT * FROM t WHERE a+b < 20;

Now, is it possible to use a function on condition field of mongoose query? Let's say I'm querying something like this:

tSchema.find({a+b: {$lt: 20}}, callback);

No, you can't create computed columns with find, so you'd have to use aggregate for this which is more flexible, but also slower.

Like this:

tSchema.aggregate([

    // Include the a, b, and a+b fields from each doc
    { $project: {
        a: 1,
        b: 1,
        sum: {$add: ['$a', '$b']}}},

    // Filter the docs to just those where sum < 20
    { $match: {sum: {$lt: 20}}}

], function(err, result) {
    console.log(result);
});

For completeness, I should note that you can do this with find and a $where filter, but the performance of that is terrible, so it isn't recommended. Like so:

tSchema.find({$where: 'this.a + this.b < 20'}, function(err, result) {
    console.log(result);
});