let's say I run this query in Mongoose:

Room.find({}, function(err,docs){

}).sort({date:-1}); 

This doesn't work!

share|improve this question
up vote 243 down vote accepted

Sorting in Mongoose has evolved over the releases such that some of these answers are no longer valid. As of the 4.1.x release of Mongoose, a descending sort on the date field can be done in any of the following ways:

Room.find({}).sort('-date').exec(function(err, docs) { ... });
Room.find({}).sort({date: -1}).exec(function(err, docs) { ... });
Room.find({}).sort({date: 'desc'}).exec(function(err, docs) { ... });
Room.find({}).sort({date: 'descending'}).exec(function(err, docs) { ... });
Room.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
Room.find({}, null, {sort: '-date'}, function(err, docs) { ... });
Room.find({}, null, {sort: {date: -1}}, function(err, docs) { ... });

For an ascending sort, omit the - prefix on the string version or use values of 1, asc, or ascending.

share|improve this answer
    
the first one did the work pretty well xD – Pedro Emilio Borrego Rached Jan 25 at 19:04
    
+1 for showing tons of different ways it can be done. However, I can't find in the docs that Query#find will take that many arguments. The signature is Query#find([criteria], [callback]). I thought maybe there was some secret handshake that says "criteria" can be up to three arguments, but it lists the type as "Object". – Nateowami Feb 17 at 14:46
    
@Nateowami You're looking at the wrong find method in the docs. See Model.find. – JohnnyHK Feb 17 at 14:49
    
You're right. I saw they were using the Module#property notation and searched for #find. It seems there is no easy way to navigate or search the docs. Searching for find yields 187 results. – Nateowami Feb 17 at 16:41

The correct answer is:

Blah.find({}).sort({date: -1}).execFind(function(err,docs){

});
share|improve this answer
9  
the updated sort syntax for the example above is: sort('-date') mongoosejs.com/docs/api.html#query_Query-sort – emilebaizel Oct 31 '12 at 20:14
    
This one did not work for me. I am getting an error "User.find(...).sort(...).execFind is not a function" – Sandip Subedi Nov 25 '16 at 23:24

Been dealing with this issue today using Mongoose 3.5(.2) and none of the answers quite helped me solve this issue. The following code snippet does the trick

Post.find().sort('-posted').find(function (err, posts) {
    // user posts array
});

You can send any standard parameters you need to find() (e.g. where clauses and return fields) but no callback. Without a callback it returns a Query object which you chain sort() on. You need to call find() again (with or without more parameters -- shouldn't need any for efficiency reasons) which will allow you to get the result set in your callback.

share|improve this answer

I do this:

Data.find( { $query: { user: req.user }, $orderby: { dateAdded: -1 } } function ( results ) {
    ...
})

This will show the most recent things first.

share|improve this answer
1  
$orderby is deprecated in MongoDB 3.2 so it shouldn't be used anymore. – JohnnyHK Jun 22 '16 at 22:12
Post.find().sort({date:-1}, function(err, posts){
});

Should work as well

share|improve this answer

See if this helps > How to sort in mongoose?

Also read this > http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order

share|improve this answer
    
That 1st method doesn't work. It just hangs...I think it's because of an update in mongoose.... And the 2nd method is just the mongo docs, which I know about. – TIMEX Apr 28 '11 at 23:25
    
The find() function is MongoDB's function, not Mongoose. Please read Mongoose API page for more details You can use the syntax defined in MongoDB docs with Mongoose. That's why Mongoose doesn't have its own sorting or intersect queries. – neebz Apr 29 '11 at 10:40

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.