get max value in mongoose

I couldn't get the other answers to work. Perhaps I'm using a newer version of Mongoose ([email protected]).

This worked for me:

Table1.findOne()
    .where({field1: 1})
    .sort('-LAST_MOD')
    .exec(function(err, doc)
    {
        var max = doc.LAST_MOD;
        // ...
    }
);

MongoDB supports max/min, but they don't recommend using it in real applications :

min and max exist primarily to support the mongos (sharding) process.

http://www.mongodb.org/display/DOCS/min+and+max+Query+Specifiers

You could pretty much have the same result with :

Model.findOne({ field1 : 1 }).sort(last_mod, 1).run( function(err, doc) {
     var max = doc.last_mod;
});