【发布时间】:2017-10-05 00:13:52
【问题描述】:
我们在 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", 坐标:[ 51.410608, 35.744042 ] },$maxDistance:2000 } } }).sort({"user.lastActivity":-1}).limit(100)
此查询在 8.486 秒内从 500204 条记录中返回 100 条记录。
我们还将 geoNear 用于以下查询:
db.runCommand( { geoNear: "用户", 附近:{类型:“点”,坐标:[51.410608, 35.744042]}, 球形:真, “限制”:100,“最大距离”:2000 })
此查询在 6.215 秒内从 500204 条记录中返回 100 条记录。此查询查找距离较近的点,但执行需要很长时间。
我们在 users.locations 字段上添加索引 2dsphere。
1) 请告诉我为什么$nearSphere 的执行时间比$nearSphere 多? 2) 如何使用 $nearSphere 找到近点并计算返回记录距离? 3)如何用更少的时间执行geoNear?
我们还在 1394018 条记录上找到了 mysql 的近点。执行时间为 0.0011。这次真是太棒了。
选择 st_distance_sphere(fld_geom, point(35.744042,51.410608)) 为 到 testgeom 的距离,其中 st_distance_sphere(fld_geom, point( 35.744042,51.410608))
我觉得mysql spatial比mongodb spatial很强大。
【问题讨论】: