mongodb,按geoNear和date排序?

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

如何在mongoDB中按邻近度和日期排序? 我试过这个但是他们只是按日期排序:

how to sort by proximity and date in mongoDB? I tried this. But they just sort by date:

coll.find({'date':{$gte:date},'location':{$nearSphere:[lat,lng]}}).sort({'date':1}).execFind(function (err, docs) {})

我感谢帮助。

推荐答案

没有直接的方法使用 $ near 或 $ nearSphere ,并按另一个字段排序,因为这两个运算符都已经排序了 find()。当您通过日期重新排序时,您将重新排序结果。然而,您可以做的是逐步获取 $ nearSphere 的结果,并对每组结果进行排序。例如:

There's no direct way to use $near or $nearSphere and sort by another field, because both of these operators already sort the results of doing a find(). When you sort again by 'date', you're re-sorting the results. What you can do, however, is grab results from the $nearSphere incrementally, and sort each set of results. For example:

function sortByDate(a, b) { return a.date - b.date; } // how many results to grab at a time var itersize = 10; // this will hold your final, two-way sorted results var sorted_results = new Array(); for (var i=0, last=db.coll.count(); i<last-itersize; i+=itersize) { var results = db.coll.find( {"date":{$gte:date}, // longitude, then latitude "location":[lng, lat]} ).skip(i).limit(itersize).toArray(); // do date sorting app-side for each group of nearSphere-sorted results sorted_results = sorted_results.concat( results.sort(sortByDate) ); }

您还应该了解mongodb查询中指定地理空间坐标的顺序。 MongoDB使用 geojson 规范,它的X,Y,Z顺序(即经度,纬度)。

You should also be aware of the order you specify geospatial coordinates in mongodb queries. MongoDB uses the geojson spec, which does coordinates in X, Y, Z order (i.e., longitude, latitude).

更多推荐

mongodb,按geoNear和date排序?

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

发布评论

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

>www.elefans.com

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