How to sort a collection by date in MongoDB ?



  • 14 Answer(s)

    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.

    Answered on January 23, 2019.
    Add Comment
    db.getCollection('').find({}).sort({_id:-1}) 
    Answered on January 26, 2019.
    Add Comment

    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.

    Answered on January 27, 2019.
    Add Comment
    collection.find().sort({datefield: -1}, function(err, cursor){...});
    Answered on January 28, 2019.
    Add Comment
    db.collection.find()._addSpecial( "$orderby", { age : -1 } )
    db.collection.find( { $query: {}, $orderby: { age : -1 } } )
    Answered on February 2, 2019.
    Add Comment

    collection.find().sort(‘date’:1).exec(function(err, doc) {});

    Answered on February 2, 2019.
    Add Comment

    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.

    Answered on February 4, 2019.
    Add Comment

    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
    })
    Answered on February 4, 2019.
    Add Comment
    db.products.find().sort({"created_at": 1}) --- 1 for asc and -1 for desc
    Answered on February 5, 2019.
    Add Comment

    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

    Answered on February 9, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.