我有以下数据:
{ "_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 ] }我想首先,按距离排序,如果距离相同,则按时间排序。
我的命令:
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})如何对“最近”和“创建日期”进行排序?
I have the following data:
{ "_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.
my command :
db.runCommand( { geoNear: "paging", near: [106.606033,29.575897 ], spherical : true, maxDistance : 1/6371, minDistance:0/6371, distanceMultiplier: 6371, num:2, query: {'_id': {'$nin': []}} })or
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有“得分”一样。
您正在使用的geoNear数据库命令也不能以这种方式与“cursor” .sort()一起使用。
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 } ])这相当于你想要做的事情。
使用聚合框架,您可以使用“管道运算符”而不是“游标修饰符”来执行$sort , $skip和$limit 。 这些也必须是逻辑顺序,而光标修饰符通常可以解决它。
这是一个“管道”,就像“Unix管道”。 |
另外,请注意“maxDistance”和“distanceMuliplier”。 由于您的坐标是“遗留坐标对”而不是GeoJSON格式,因此距离以“弧度”来衡量。 如果您有GeoJSON存储的位置数据,则结果以“米”返回。
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".
更多推荐
发布评论