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)
The limit() method in Mongoose is used to specify the number or a maximum number of documents to return from a query.
It works perfectly fine.
Model.findOne().sort({
"field": -1
});
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With