首页 文章

Mongodb发现附近的用户指向距离

提问于
浏览
0

我们有一个问题,在mongodb数据库中找到附近用户点的距离 . 我们的主要问题是我们不能用距离聚集近点 . 我们在下面的查询中使用了$ geoWithin:

db.users.find({location:{$ geoWithin:{$ centerSphere:[[51.410608,35.744042],2 / 6378.1]}}}) . sort({“user.lastActivity”: - 1}) . limit( 100)

此查询在0.005秒内从500204条记录中返回100条记录 .

我们还在下面的查询中使用$ nearSphere:

db.users.find({location:{$ nearSphere:{$ geometry:{type:“Point”,coordinates:[51.410608,35.744042]},$ maxDistance:2000}}}) . sort({“user.lastActivity “:-1})极限(100)

此查询从8.486秒返回500204条记录中的100条记录 .

我们还使用了以下查询的geoNear:

db.runCommand({geoNear:“users”,near:{type:“Point”,coordinates:[51.410608,35.744042]},spherical:true,“limit”:100,“maxDistance”:2000})

此查询从6.215秒返回500204条记录中的100条记录 . 此查询找到距离近点,但执行需要很长时间 .

我们在users.locations字段上添加索引2dsphere .

1)请告诉我为什么$ nearSphere执行时间超过$ nearSphere? 2)如何用$ nearSphere找到近点并计算返回记录距离? 3)如何用更少的时间执行geoNear?

我们还在1394018记录中找到了与mysql的近点 . 执行时间是0.0011 . 这一次非常棒 .

SELECT st_distance_sphere(fld_geom,point(35.744042,51.410608))作为距testgeom的距离,其中st_distance_sphere(fld_geom,point(35.744042,51.410608))<= 2000 LIMIT 100

我认为mysql空间比mongodb空间非常强大 .

2 回答

  • 0

    要提高性能,请在要搜索的字段上为集合创建2D球体索引 .

    https://docs.mongodb.com/manual/core/2dsphere/#create-a-2dsphere-index

    还有其他类型的地理空间索引 . 这应该以内存利用率为代价大幅提高性能 .

  • 0

    $ nearSphere为您提供按距离排序的结果,而$ getWithin则不是 . 我不认为sql查询按距离对文档进行排序(只需像$ geoWithin一样过滤它们)

    • 使用$ nearSphere对user.lastActivity进行排序没有意义 - 你只需覆盖$ nearSphere的距离排序 .

    来自documentation

相关问题