Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (5k points)

I have a mongodb collection like:

db.kids.find()

//results

[

    {name:'tom', age:10},

    {name:'alice', age:12},

    ....

]

I want to have query to get MAX 'age' from this collection like in SQL: SELECT MAX(age) FROM kids WHERE 1

1 Answer

0 votes
by (10.2k points)

Please use the following query:

db.collection.find().sort({age:-1}).limit(1) // for MAX

db.collection.find().sort({age:+1}).limit(1) // for MIN

Check out this MongoDB tutorial to find more insights into the concepts.

Browse Categories

...