0

Sorting of records gives irreverent results I am using mongoDB with sails.js

Here is the syntax:

 model.find(conditions).sort({ "title": -1 }) 
Naresh Walia
  • 2,012
  • 2
  • 16
  • 16

2 Answers2

3

Sails supports native MongoDB queries so yo can use MongoDB Aggregation framework.

model.native(function(err, collection) {
    collection.aggregate(
        //...
    );
});

Take a look here http://docs.mongodb.org/manual/applications/aggregation/

0

Use can use the lodash global to perform the sort instead, like this:

model.find(conditions).exec(function(err, records) {
    _.sortBy(records, function(blah){ return your_custom_logic_here; });
});
amarprabhu
  • 350
  • 3
  • 10
  • 2
    Don't think it's a good solution. If database will contain about a million records you will fetch all records first and then will perform sorting. It will require lot of memory and will create issues with loading time. – Konstantin Zolotarev Nov 11 '14 at 21:00
  • Agreed. But the default sort will not allow the OP to perform the custom action. SO scratching my head... – amarprabhu Nov 12 '14 at 08:12