Lazov
Lazov

Reputation: 1476

MongoDB sort & limit problems

My problem is that I want to return the docs of the last five created entries in the database. I know that something like:

return Events.find({}, {sort: {createdOn: -1, limit: 5}});

should work but it does not.

Upvotes: 1

Views: 58

Answers (1)

chridam
chridam

Reputation: 103305

The sort specifier should be a separate property to the limit option:

return Events.find({}, {sort: {createdOn: -1}, limit: 5});

Upvotes: 3

Related Questions