0

Say I have a URL like this:

http://dev.myserver.com/systems?system_type.category=Penetration

which hits the following controller:

exports.findAll = function(req, res) { 
    System.find(req.query, function(err, systems) {
        res.send(systems);
    });
};

and then returns the set below 'auto-magically' using Node, Express, MongoDB and Mongoose:

[{
    "_id":            "529e5f29c128685d860b3bad",
    "system_number":  "123",
    "target_country": "USA",
    "system_type":     {
        "category":   "Penetration",
        "subcategory": ["Floor", "Wall"]
    }
},{
    "_id":            "999e5f29c128685d860b3bad",
    "system_number":  "456",
    "target_country": "Canada",
    "system_type":     {
        "category":   "Penetration",
        "subcategory": ["Floor", "Wall"]
    }
}]

Now, if I want the results sorted by 'target_country', what is the 'best practice' for 'auto-magically' doing that?

Are there certain parameters/syntax that Mongoose/Express are expecting to do it for me? Or, is this a case where I have to specifically code for it? (That would kill the 'auto-magical' functionality already there.)

Thanks!

UPDATE: Here is what worked for me.

exports.findAll = function(req, res) {

    // Sort Ascending: 
    http://dev.dom.com/systems?system_type.category=Penetration&sort=system_number

    // Sort Descending:
    http://dev.dom.com/systems?system_type.category=Penetration&sort=-system_number

    // Default sort ascending on system_number:
    http://dev.dom.com/systems?system_type.category=Penetration

    var sort_param = req.query.sort ? req.query.sort : 'system_number';

    System.find().sort(sort_param).find(function(err, menus) {        
        res.send(menus);
    });

};

I guess where I went wrong, was to think I should find with filters and then sort, instead of find all, sort and then find again with filters. Still getting my head wrapped around the whole 'callback philosophy' I guess.

Eric
  • 669
  • 3
  • 10
  • 24
  • possible duplicate of [In Mongoose, how do I sort by date? (node.js)](http://stackoverflow.com/questions/5825520/in-mongoose-how-do-i-sort-by-date-node-js) – JohnnyHK Jan 03 '14 at 00:02
  • Not a duplicate. In the other thread, the sort parameter is known to be 'date'. Here, I don't know what the sort parameter provided in the GET request query parameters will be. I suggest it is the 'target_country' but it could be any valid field. (I only show a few in the example for clarity - there are more.) – Eric Jan 03 '14 at 01:19

1 Answers1

1

You need to define separate URL parameters for the query and sort components of your System query. As in:

System.find(req.query.query).sort(req.query.sort).exec(function(err, systems) {
    res.send(systems);
});

Then you'd use request URL parameters that look like:

?sort=target_country
    &query[system_type.category]=Penetration
    &query[system_type.subcategory]=Floor

Docs on sort here.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • Well, if I edit the http request to be: http://dev.myserver.com/systems?system_type.category=Penetration&sort=system_number then I would need to use .sort(req.query.sort) but that fails, likely due to a 'bigger' problem. It seems even using the controller method I defined (without the sort) fails when the sort parameter is in the query. I get an empty set. Clearly I don't understand the System.find(re.query) yet. Ideas? – Eric Jan 03 '14 at 13:55
  • @Eric OK, I see what you're trying to do now. If you use the entire `req.query` as your Mongoose query object, then you don't have any way to separately specify a sort parameter. You need to define separate URL parameters for the query and the sort. See updated answer. – JohnnyHK Jan 03 '14 at 15:13
  • I've updated my question to include the answer. I now realize I didn't form the original question properly. Sorry to JohnnyHK if I confused you. Thanks for your help steering me most of the way to the correct answer. – Eric Jan 03 '14 at 19:22