Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

I can't find anywhere it has been documented this. By default, the find() operation will get the records from the beginning. How can I get the last N records in MongoDB?

1 Answer

0 votes
by (106k points)

From your question it looks like, you want to sort in ascending order:-

For that, you can use sort() function:-

db.foo.find().sort({x:1});

If you pass the 1 as a parameter then it will sort in ascending order and if you pass -1 then it will sort in descending order.

Related questions

0 votes
2 answers
0 votes
1 answer
asked Sep 4, 2019 in SQL by Sammy (47.6k points)
0 votes
2 answers
0 votes
0 answers
asked Jun 24, 2021 in SQL by Harsh (1.5k points)
0 votes
1 answer
asked May 6, 2020 in SQL by Sudhir_1997 (55.6k points)

Browse Categories

...