使用Mongodb的$geoIntersects判断两个经纬度组成的矩形是否相交,按照文档描述书写无法得到正确结果
复现方法:
插入一条数据
db.getCollection("1").insertOne({"box": {"type": "Polygon","coordinates": [[[109.052708152174,32.5329883043478],[109.052708152174,46.7784766086957],[130.576614304348,46.7784766086957],[130.576614304348,32.5329883043478],[109.052708152174,32.5329883043478]]]}
})
查询是否相交
db.getCollection("1").find({"box":{$geoIntersects: {$geometry: {type: "Polygon" ,coordinates: [[[117.390449, 32.37105],[117.390449, 32.598390499999994],[117.486579, 32.598390499999994],[117.486579, 32.37105],[117.390449, 32.37105]]],}}}}
)
得到的结果为空
网上搜索 得到的结果寥寥无几 看到stackoverflow上有人说可以加上一个CRS参数
db.getCollection("1").find({"box":{$geoIntersects: {$geometry: {type: "Polygon" ,coordinates: [[[117.390449, 32.37105],[117.390449, 32.598390499999994],[117.486579, 32.598390499999994],[117.486579, 32.37105],[117.390449, 32.37105]]],crs: {type: "name",properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }}}}}}
)
竟然就可以了
搜了半天也不知道这个具体什么原理,只查到是按照某个指定的参考系来判断地理位置