Mongo ordering a $near with another secondary sort Mongo ordering a $near with another secondary sort mongoose mongoose

Mongo ordering a $near with another secondary sort


Presuming your data is actually properly arranged for MongoDB and looks something like this:

{    "shop": 1,    "usecount": 12,    "closest": 3,    "geo": {        "type": "Point",        "coordinates": [1333.222,222.222]    }}

And your coordinates are in fact in "longitude/latitude" order as is requireed from GeoJSON and MongoDB and that you have a geospatial index that is "2dsphere", then your best option for "composite sort" is using the $geoNear aggregate command pipeline, along with aggregation $sort:

Model.aggregate(    [        { "$geoNear": {            "near": {               "type": "Point",                "coordinates": [1333.222,222.222]            },            "distanceField": "dist",            "spherical": true        }},        { "$sort": { "dist": 1, "usecount": -1 } }    ],    function(err,results) {    })

Where the $geoNear projects the "distance" as the nominated field here in "dist", and then you use that in the $sort along with the other field "usecount" as shown here in descending order for the "largest" value if "usecount" first, and within each "dist" already sorted.

The aggregation framework though .aggregate() does more than just "aggregate" documents. It is your "main tool" for projecting new values into a document, useful for such things as sorting results by values that "calculate" by one means or the other.

Unlike $near ( or $nearSphere ) the distance is returned as a true field in the document rather than just a "default" sort order. This allows that key to be used in the sorted results, along with any other field value present or projected into the document at the $sort stage.

Also noting that your data here does not appear to be valid spherical coordinates, which is going to cause problems with GeoJSON storage and also a "2dsphere" index. If not real global coordinates but coordinates on a "plane, then just use a plane legacy array for "geo" as [1333.222,222.222] and a "2d" index only. As well the argument to "near" within $geoNear is simply an array as well, and the "spherical" option would then not be required.

But possibly a problem with typing in your question as well.