0

Basically, I have the same question with this post: How to get the last N records in mongodb?

I saw the solution using sort() but would it be inefficient because it requires loading all of the data into memory, which can be slow and resource-intensive?

Would there be another way to solve this without having to use sort()?

Tui
  • 105
  • 1
  • 14
  • 1
    **it requires loading all of the data into memory* - Where do you read this from? – ray Feb 20 '23 at 02:47
  • I apologize if it's not correct. I asked ChatGPT and it told me that. – Tui Feb 20 '23 at 02:50
  • 2
    No need to be sorry :) Just remember ChatGPT may not always produce accurate truth and it is the users' responsibility to verify the outcome of chatGPT. Regarding the statement of loading all of the data into memory, It might be ambiguous to judge its correctness as we don't know what *data* it is referring to. In practice, **with proper indexing**, sort() is probably the most canonical way to achieve what you want. You may want to check out [this official doc](https://www.mongodb.com/docs/manual/tutorial/sort-results-with-indexes/) for more details as a starting point first. – ray Feb 20 '23 at 03:15
  • Thank you so much @ray!! Yeah, it kept giving wrong results so I don't really believe it much. But somehow it confirmed my bias so I thought it was right for a second. My mistake. I appreciate your reply and the link. I'm looking into it right now! – Tui Feb 20 '23 at 03:28

0 Answers0