How to sort a collection by date in MongoDB ?
How to sort a collection by date in MongoDB ?
Sorting by date doesn’t require anything special. Just sort by the desired date field of the collection.
Updated for the 1.4.28 node.js native driver, you can sort ascending on datefield using any of the following ways:
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 in place of the 1.
To sort descending, use ‘desc’, ‘descending’, or -1 in place of the 1.
Sorting by date doesn’t require anything special. Just sort by the desired date field of the collection.
Updated for the 1.4.28 node.js native driver, you can sort ascending on datefield using any of the following ways:
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 in place of the 1.
To sort descending, use ‘desc’, ‘descending’, or -1 in place of the 1.
collection.find().sort({datefield: -1}, function(err, cursor){...});
db.collection.find()._addSpecial( "$orderby", { age : -1 } )
db.collection.find( { $query: {}, $orderby: { age : -1 } } )
collection.find().sort(‘date’:1).exec(function(err, doc) {});
Sorting by date doesn’t require anything special. Just sort by the desired date field of the collection.
Updated for the 1.4.28 node.js native driver, you can sort ascending on datefield using any of the following ways:
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 in place of the 1.
To sort descending, use ‘desc’, ‘descending’, or -1 in place of the 1.
With mongoose I was not able to use ‘toArray’, and was getting the error: TypeError: Collection.find(...).sort(...).toArray is not a function.
The toArray function exists on the Cursor class from the Native MongoDB NodeJS driver (reference).
Also sort accepts only one parameter, so you can’t pass your function inside it.
This worked for me (as answered by Emil):
collection.find().sort('-date').exec(function(error, result) {
// Your code
})
db.products.find().sort({"created_at": 1}) --- 1 for asc and -1 for desc
Sorting by date doesn’t require anything special. Just sort by the desired date field of the collection.
Updated for the 1.4.28 node.js native driver, you can sort ascending on datefield
using any of the following ways:
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 in place of the 1
.
To sort descending, use 'desc'
, 'descending'
, or -1
in place of the 1
db.collection_name.find().sort({KEY : 1})