Take the 2-minute tour ×

how to get maximum value in mongoose query . in SQL it is easy

Where field1=1

i want Equivalent of above SQL code in mongoose(node.js)

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.


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;
share|improve this answer
I think you'd want sort(last_mod, -1) instead to get max; the ascending sort above would give the min. –  JohnnyHK May 14 '12 at 1:45

I couldn't get the other answers to work. Perhaps I'm using a newer version of Mongoose (mongoose@3.0.1).

This worked for me:

    .where({field1: 1})
    .exec(function(err, doc)
        var max = doc.LAST_MOD;
        // ...
share|improve this answer

Your Answer


