Sort mongod results with javascript/nodejs method

I am using nodejs with the matador mvc framework.#

I have this method to query monogodb

  .methods({
        getPopular: function(popular) {
          shows.find({date: '2012-07-01'}, popular);
      }
  });
};

This works fine. My problem is I don't know how sort the results.

I have tried

  .methods({
        getPopular: function(popular) {
          shows.find({date: '2012-07-01'}, popular).sort({show : 1});
      }
  });
};

Please help. Thanks.

The way sort works is by sending the key to sort by, and either descending or ascending (-1 or 1)

So using your example to sort by show in a descending matter, you would do the following:

.methods({
        getPopular: function(popular) {
          shows.find({date: '2012-07-01'}, popular).sort({show : -1});
      }
  });

Mongo documentation explains a bit better:

http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order

I believe you have to include the sort keyword in the query options in the Node.js driver.

.methods({
    getPopular: function(popular) {
        shows.find({date: '2012-07-01'},{sort:[['show','desc']]}, popular);
    }
});

There is a double array ([['show','desc']]) because sort takes an array of sorting preferences as explained here. If you want ascending order, you should use asc instead.