How sort results in a nodejs – mongodb search, but, by calling a dynamic method

I’m developing a web app in nodejs connected to mongodb via mongo native connector.

In one of my js files, I have a generic method to invoke a “find” or “findOne” operation to retrieve whatever I need from a mongodb collection, like this:

It works fine for me.

But now, I need to sort the results, and as far as I know, Mongodb use the “sort” method to achieve this.

collection.ensureIndex(indexedFields, function(error, indexName) {
    if (error) {
    } else {
        var operation = ( ? collection.findOne : collection.find;, params.selector, params.fields, params.options,
            function(error, result){
                if (error) {
                } else {

In an simple query, this should be like this:
For example:

collection.find().sort({field : 1}),

I don’t know how to call “sort” method, doing it im my generic way.

Any ideas?


Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Have you looked into mongojs ( ? I use this for my node apps and it works nicely (I also use expressjs). You can do a simple find and sort by with the following syntax:

db.test.find().sort({name:1}, function(error, tuples) { console.log(tuples) });

Solution 2

Can’t you just do the same thing to the output of your call?:, params.selector, params.fields, params.options,
            function(error, result){
        ).sort({field: 1});

If the sort can only be applied to the results of find and not findOne, then it’s a bit more complicated:

    var operation = ( ? collection.findOne : function() {
        var findResults = collection.find.apply(this, [];
        return findResults.sort({field: 1});

(That last is entirely untested, of course, but looks like it should be close.)

Solution 3

using cursors is the best solution because the sorting is processeced in mongodb engine

var grades = db.collection('data');

var cursor = grades.find();
// cursor.skip(1);
// cursor.limit(4);
// cursor.sort(['State', 1]);
cursor.sort( [['Temperature', 'desc'],['State','asc']] );

//var options = { 'skip' : 1,
//                'limit' : 4,
//                'sort' : [['grade', 1], ['student', -1]] };
//var cursor = grades.find({}, {}, options);

cursor.each(function(err, doc) {
    if(err) throw err;
    if(doc == null) {
        return db.close();

Solution 4

I have an articles model where I want to search by category and get the latest article based on the created date and this is how I do it

const query = {category: "category1"};
Articles.find(query, callback).sort({created_date: -1});

Solution 5

It should be almost like Scott said.

The first method does not work, cause sort is called by an undefined object.
I think it takes sense, because there’s a callback which is performed first..
The second method is almost what i need, but not working, once again, findResults.sort returns an undefined.

I finally did something more easy, what it seems to succeed, but… there’s still something missing:, params.selector, params.fields, params.options,               
   function(error, result){
         if (err) ....
         else results.sort({field:1}, callback(err, sortedResults))

well, It pauses at “results.sort”, and it remains waiting… I don’t know what.
Looking at mongodb console, there’s no query launched. In the other hand, callback exists as a function, but not called…

Then, I tried to do this in another way;

var sortedResults = results.sort({field:1};
callback(err, sortedResults)

In this case, the execution goes on, but, it returns unsorted results.

Any other ideas? It has to be there….


Solution 6

I’ve found the solution.

It seems that “toArray” was missing, otherwise does not work., params.selector, params.fields, params.options, function(error, result)   {
if (error) {
} else {
        result.sort(params.sort).toArray(function(error, items) {

I Hope it will be helpful

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply