Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k 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 (11.7k 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.

Related questions

0 votes
1 answer
0 votes
4 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...