Geonear sort by distance and time

假如想象 提交于 2019-12-04 16:47:47

The correct query to use here uses the aggregation framework which has the $geoNear pipeline stage to assist with this. It's also the only place you get to "sort" by multiple keys, as unforntunately the "geospatial" $nearSphere does not have a "meta" projection for "distance" like $text has a "score".

Also the geoNear database command you are using can also not be used with "cursor" .sort() in that way either.

db.paging.aggregate([
    { "$geoNear": {
        "near": [106.606033,29.575897 ],
        "spherical": true,
        "distanceField": "distance",
        "distanceMuliplier": 6371,
        "maxDistance": 1/6371
    }},
    { "$sort": { "distance": 1, "createdate": -1 } },
    { "$skip": ( 2-1 ) * 2 },
    { "$limit": 5 }
])

That is the equivalent of what you are trying to do.

With the aggregation framework you use the "pipeline operators" instead of "cursor modifiers" to do things like $sort, $skip and $limit. Also these must be in a Logical order, whereas the cursor modifiers generally work it out.

It's a "pipeline", just like "Unix pipe". |

Also, be careful with "maxDistance" and "distanceMuliplier". Since your co-ordinates are in "legacy co-ordinate pairs" and not GeoJSON format, then the distances are measured in "radians". If you have GeoJSON stored location data then the result is returned in "meters".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!