How to get the last N records in mongodb?

If I understand your question, you need to sort in ascending order. Assuming you have some id or date field called “x” you would do … .sort() db.foo.find().sort({x:1}); The 1 will sort ascending (oldest to newest) and -1 will sort descending (newest to oldest.) If you use the auto created _id field it has a … Read more

tech