Sorting by date does not require anything special. Just select the desired collection date field.
Updated for the built-in driver 1.4.28 node.js, you can sort ascending on datefield any of the following methods:
collection.find().sort({datefield: 1}).toArray(function(err, docs) {...}); collection.find().sort('datefield', 1).toArray(function(err, docs) {...}); collection.find().sort([['datefield', 1]]).toArray(function(err, docs) {...}); collection.find({}, {sort: {datefield: 1}}).toArray(function(err, docs) {...}); collection.find({}, {sort: [['datefield', 1]]}).toArray(function(err, docs) {...});
'asc' or 'ascending' can also be used instead of 1 .
To sort in descending order, use 'desc' , 'descending' or -1 instead of 1 .
JohnnyHK Dec 12 '12 at 21:11 2012-12-12 21:11
source share