233

I find no documentation for the sort modifier. The only insight is in the unit tests: spec.lib.query.js#L12

writer.limit(5).sort(['test', 1]).group('name')

But it doesn't work for me:

Post.find().sort(['updatedAt', 1]);
iwein
  • 25,788
  • 10
  • 70
  • 111
Philippe Rathé
  • 8,888
  • 3
  • 22
  • 13

21 Answers21

239

In Mongoose, a sort can be done in any of the following ways:

    Post.find({}).sort('test').exec(function(err, docs) { ... });
    Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
    Post.find({}).sort({test: 1}).exec(function(err, docs) { ... });
    Post.find({}, null, {sort: {date: 1}}, function(err, docs) { ... });
Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
iwein
  • 25,788
  • 10
  • 70
  • 111
  • 8
    This is almost a straight copy of the answer linked by Francisco Presencia. Unfortunately the highest voted answers are outdated and unnecessarily long. – iwein Aug 05 '15 at 06:28
  • 4
    This is not quite correct as of today. `{sort: [['date', 1]]}` will not work, but `.sort([['date', -1]])` will work. See this answer: http://stackoverflow.com/a/15081087/404699 – steampowered Apr 30 '16 at 21:52
  • @steampowered thanks, i'll make an edit, you are very welcome to let me know or edit if I got it wrong. – iwein Nov 21 '17 at 14:56
  • 4
    Would love to see a few remarks on what the result is, esp. what the `1` and `-1` values mean, instead of having 4 code lines without much context – Philipp Dec 29 '21 at 16:39
162

This is how I got sort to work in mongoose 2.3.0 :)

// Find First 10 News Items
News.find({
    deal_id:deal._id // Search Filters
},
['type','date_added'], // Columns to Return
{
    skip:0, // Starting Row
    limit:10, // Ending Row
    sort:{
        date_added: -1 //Sort by Date Added DESC
    }
},
function(err,allNews){
    socket.emit('news-load', allNews); // Do something with the array of 10 objects
})
Derek Bredensteiner
  • 2,906
  • 4
  • 23
  • 13
104

As of Mongoose 3.8.x:

model.find({ ... }).sort({ field : criteria}).exec(function(err, model){ ... });

Where:

criteria can be asc, desc, ascending, descending, 1, or -1

Note: Use quotation marks or double quote

use "asc", "desc", "ascending", "descending", 1, or -1

Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
user3784764
  • 1,041
  • 1
  • 7
  • 2
78

UPDATE:

Post.find().sort({'updatedAt': -1}).all((posts) => {
  // do something with the array of posts
});

Try:

Post.find().sort([['updatedAt', 'descending']]).all((posts) => {
  // do something with the array of posts
});
Syntle
  • 5,168
  • 3
  • 13
  • 34
Eric
  • 781
  • 4
  • 2
49

Mongoose v5.x.x

sort by ascending order

Post.find({}).sort('field').exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'asc' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'ascending' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 1 }).exec(function(err, docs) { ... });

Post.find({}, null, {sort: { field : 'asc' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'ascending' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 1 }}), function(err, docs) { ... });

sort by descending order

Post.find({}).sort('-field').exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'desc' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'descending' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: -1 }).exec(function(err, docs) { ... });


Post.find({}, null, {sort: { field : 'desc' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'descending' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : -1 }}), function(err, docs) { ... });

For Details: https://mongoosejs.com/docs/api.html#query_Query-sort

Sultan Aslam
  • 5,600
  • 2
  • 38
  • 44
  • As @Sunny Sultan pointed portend out all `Post.find({}, null, {sort: '-field'}, function(err, docs) { ... });` for descending and `Post.find({}, null, {sort: 'field'}, function(err, docs) { ... });` for ascending work as well. In Fact as I pass the field and sort order in the http query parameters, combining them into a string and use this way to sort my bd query is my only option. – Vincenzo Jun 02 '21 at 18:22
24

Update

There is a better write up if this is confusing people; check out finding documents and how queries work in the mongoose manual. If you want to use the fluent api you can get a query object by not providing a callback to the find() method, otherwise you can specify the parameters as I outline below.

Original

Given a model object, per the docs on Model, this is how it can work for 2.4.1:

Post.find({search-spec}, [return field array], {options}, callback)

The search spec expects an object, but you can pass null or an empty object.

The second param is the field list as an array of strings, so you would supply ['field','field2'] or null.

The third param is the options as an object, which includes the ability to sort the result set. You would use { sort: { field: direction } } where field is the string fieldname test (in your case) and direction is a number where 1 is ascending and -1 is desceding.

The final param (callback) is the callback function which receives the collection of docs returned by the query.

The Model.find() implementation (at this version) does a sliding allocation of properties to handle optional params (which is what confused me!):

Model.find = function find (conditions, fields, options, callback) {
  if ('function' == typeof conditions) {
    callback = conditions;
    conditions = {};
    fields = null;
    options = null;
  } else if ('function' == typeof fields) {
    callback = fields;
    fields = null;
    options = null;
  } else if ('function' == typeof options) {
    callback = options;
    options = null;
  }

  var query = new Query(conditions, options).select(fields).bind(this, 'find');

  if ('undefined' === typeof callback)
    return query;

  this._applyNamedScope(query);
  return query.find(callback);
};

HTH

AJ.
  • 3,062
  • 2
  • 24
  • 32
  • for projection : we need to provide string that contains column names seperated with space. – maddy Nov 27 '15 at 08:02
18

you can sort your query results by

Post.find().sort({createdAt: "descending"});
12

This is how I got sort to work in mongoose.js 2.0.4

var query = EmailModel.find({domain:"gmail.com"});
query.sort('priority', 1);
query.exec(function(error, docs){
  //...
});
Salida Software
  • 446
  • 5
  • 6
11

Chaining with the query builder interface in Mongoose 4.

// Build up a query using chaining syntax. Since no callback is passed this will create an instance of Query.
var query = Person.
    find({ occupation: /host/ }).
    where('name.last').equals('Ghost'). // find each Person with a last name matching 'Ghost'
    where('age').gt(17).lt(66).
    where('likes').in(['vaporizing', 'talking']).
    limit(10).
    sort('-occupation'). // sort by occupation in decreasing order
    select('name occupation'); // selecting the `name` and `occupation` fields


// Excute the query at a later time.
query.exec(function (err, person) {
    if (err) return handleError(err);
    console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host
})

See the docs for more about queries.

7
app.get('/getting',function(req,res){
    Blog.find({}).limit(4).skip(2).sort({age:-1}).then((resu)=>{
        res.send(resu);
        console.log(resu)
        // console.log(result)
    })
})

Output

[ { _id: 5c2eec3b8d6e5c20ed2f040e, name: 'e', age: 5, __v: 0 },
  { _id: 5c2eec0c8d6e5c20ed2f040d, name: 'd', age: 4, __v: 0 },
  { _id: 5c2eec048d6e5c20ed2f040c, name: 'c', age: 3, __v: 0 },
  { _id: 5c2eebf48d6e5c20ed2f040b, name: 'b', age: 2, __v: 0 } ]
Jaskaran Singh
  • 2,392
  • 24
  • 39
5

with the current version of mongoose (1.6.0) if you only want to sort by one column, you have to drop the array and pass the object directly to the sort() function:

Content.find().sort('created', 'descending').execFind( ... );

took me some time, to get this right :(

Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
4

This is how I managed to sort and populate:

Model.find()
.sort('date', -1)
.populate('authors')
.exec(function(err, docs) {
    // code here
})
Mark Nguyen
  • 7,168
  • 9
  • 31
  • 41
3
Post.find().sort({updatedAt: 1});
chovy
  • 72,281
  • 52
  • 227
  • 295
3

As of October 2020, to fix your issue you should add .exec() to the call. don't forget that if you want to use this data outside of the call you should run something like this inside of an async function.

let post = await callQuery();

async function callQuery() {
      return Post.find().sort(['updatedAt', 1].exec();
}
Frederick Haug
  • 245
  • 2
  • 14
2
Post.find().sort({updatedAt:1}).exec(function (err, posts){
...
});
gtsouk
  • 5,208
  • 1
  • 28
  • 35
2

Starting from 4.x the sort methods have been changed. If you are using >4.x. Try using any of the following.

Post.find({}).sort('-date').exec(function(err, docs) { ... });
Post.find({}).sort({date: -1}).exec(function(err, docs) { ... });
Post.find({}).sort({date: 'desc'}).exec(function(err, docs) { ... });
Post.find({}).sort({date: 'descending'}).exec(function(err, docs) { ... });
Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
Post.find({}, null, {sort: '-date'}, function(err, docs) { ... });
Post.find({}, null, {sort: {date: -1}}, function(err, docs) { ... });
Sunny Sultan
  • 1,090
  • 15
  • 20
2

Others worked for me, but this did:

  Tag.find().sort('name', 1).run(onComplete);
FrederickCook
  • 3,018
  • 4
  • 24
  • 26
1

This is what i did, it works fine.

User.find({name:'Thava'}, null, {sort: { name : 1 }})
Thavaprakash Swaminathan
  • 6,226
  • 2
  • 30
  • 31
1
// Ascending with updatedAt field
Post.find().sort('updatedAt').exec((err, post) => {...});

// Descending with updatedAt field
Post.find().sort('-updatedAt').exec((err, post) => {...});

Refer here: https://mongoosejs.com/docs/queries.html

0

you can also use aggregate() for sorting

 const sortBy = req.params.sort;
  const limitNum = req.params.limit;
  const posts = await Post.aggregate([
    { $unset: ['field-1', 'field-2', 'field-3', 'field-4'] },
    { $match: { field-1: value} },
    { $sort: { [sortBy]: -1 } },  //-------------------> sort the result
    { $limit: Number(limitNum) },
  ]);
Marzieh Mousavi
  • 1,213
  • 1
  • 12
  • 28
-1

Solution :

posts.find().sort({field:1})  

// for ascending and for descending order just use -1 instead of 1

Iva
  • 2,447
  • 1
  • 18
  • 28