How do you tell Mongo to sort a collection before limiting the results?

The problem is that you need to sort on date instead of $date.

myCollection.find().sort({date: 1}).limit(50, callback);

Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor.

Proof in docs: link

db.bios.find().sort( { name: 1 } ).limit( 5 )
db.bios.find().limit( 5 ).sort( { name: 1 } )

The two statements are equivalent; i.e. the order in which you chain the limit() and the sort() methods is not significant. Both statements return the first five documents, as determined by the ascending sort order on ‘name’.

Tags:

Mongodb