Geonear按距离和时间排序

编程入门 行业动态 更新时间:2024-10-14 02:19:18
本文介绍了Geonear按距离和时间排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下数据:

{ "_id" : ObjectId("55a8c1ba3996c909184d7a22"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:02.892Z"), "palce" : "aa", "sex" : 1, "longdis" : 1, "location" : [ 106.607312, 29.575281 ] } { "_id" : ObjectId("55a8c1ba3996c909184d7a24"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:02.920Z"), "palce" : "bbb", "sex" : 1, "longdis" : 1, "location" : [ 106.589896, 29.545098 ] } { "_id" : ObjectId("55a8c1ba3996c909184d7a25"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:02.922Z"), "palce" : "ccc", "sex" : 1, "longdis" : 1, "location" : [ 106.590758, 29.566713 ] } { "_id" : ObjectId("55a8c1ba3996c909184d7a26"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:02.923Z"), "palce" : "ddd", "sex" : 1, "longdis" : 1, "location" : [ 106.637039, 29.561436 ] } { "_id" : ObjectId("55a8c1bc3996c909184d7a27"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:04.499Z"), "palce" : "eee", "sex" : 1, "longdis" : 1, "location" : [ 106.539522, 29.57929 ] } { "_id" : ObjectId("55a8d12e78292fa3837ebae4"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T09:55:58.947Z"), "palce" : "fff", "sex" : 1, "longdis" : 1, "location" : [ 106.637039, 29.561436 ] }

我想首先按距离排序,如果距离相同,则按时间排序.

I want to first of all, sort by the distance, if the distance is the same, sort by the time.

我的命令:

db.runCommand( { geoNear: "paging", near: [106.606033,29.575897 ], spherical : true, maxDistance : 1/6371, minDistance:0/6371, distanceMultiplier: 6371, num:2, query: {'_id': {'$nin': []}} })

db.paging.find({ 'location':{ $nearSphere: [106.606033,29.575897], $maxDistance:1 } }).limit(5).skip((2 - 1) * 2).sort({createdate:-1})

如何对最近"和创建日期"进行排序?

How can I sort on both "nearest" and "createddate"?

推荐答案

此处使用的正确查询使用聚合框架具有 $geoNear 流水线阶段对此提供协助.这也是您唯一可以通过多个键排序"的地方,因为不幸的是,地骨" $nearSphere没有距离"的元"投影,就像$text的得分"一样.

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".

同样,您正在使用的geoNear数据库命令也不能以这种方式与"cursor" .sort()一起使用.

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.

通过聚合框架,您可以使用管道运算符" 代替光标修饰符"可用于执行$sort,$skip和$limit之类的操作.同样,这些必须必须按逻辑顺序排列,而光标修饰符通常可以计算出来.

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.

这是一个管道",就像"Unix管道"一样. |

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

此外,请注意"maxDistance"和"distanceMuliplier".由于您的坐标是旧版坐标对"而不是GeoJSON格式,因此距离以弧度"为单位.如果您有GeoJSON存储的位置数据,则结果以米"为单位返回.

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".

更多推荐

Geonear按距离和时间排序

本文发布于:2023-10-18 15:03:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:距离   时间   Geonear

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!