Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get max value in mongoose

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

SELECT MAX(LAST_MOD) FROM table1
Where field1=1

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

like image 795
MajidTaheri Avatar asked May 13 '12 14:05

MajidTaheri


People also ask

What is limit in mongoose?

The limit() method in Mongoose is used to specify the number or a maximum number of documents to return from a query.


2 Answers

It works perfectly fine.

Model.findOne().sort({
        "field": -1
    });
like image 168
Artem Balamutyuk Avatar answered Oct 03 '22 00:10

Artem Balamutyuk


You can use this, for returning one record in collection (for example Goods):

Goods
   .find({})
   .select({"price"})
   .sort({"price" : -1})
   .limit(1)
   .exec(function(err, doc){
      let max_price = doc[0].price;
});