我正在尝试使用 mongoDb 的 $geoNear 聚合运算符以下列方式计算用户与当前位置的距离:
'$geoNear': {
near: currentLocation,
distanceField: 'distance',
spherical: true,
}
currentLocation 类似于:
{ "type" : "Point", "coordinates" : [ -122.1575745, 37.4457966 ] }
我的收藏属于以下类型(使用猫鼬):
users = [{
....
location : { // GeoJSON Point or I think it is ;)
type: {
type: String
},
coordinates: []
}
....
}]
我正在使用索引(再次使用猫鼬的语法):
userSchema.index({
location: '2dsphere'
});
现在我面临的问题是,如果我使用上面提到的 currentLocation 查询(以 GeoJSON 的形式)我会得到奇怪的距离(非常大的数字),但是如果我使用 currentLocation.coordinates,即使用旧坐标对([-122.1575745 , 37.4457966]),我得到了正确的结果。但是geoNear 的 mongoDb 文档清楚地表明我们可以使用GeoJSON 点或旧坐标对进行查询。
我很想知道 GeoJSON 点和旧坐标对之间到底有什么区别?
例如收集:
{ "_id" : ObjectId("5277679914c6d8f00b000003"), "location" : { "type" : "Point", "coordinates" : [ 106.6202887, -6.1293536 ] } }
{ "_id" : ObjectId("5277810148219d011c000003"), "location" : { "type" : "Point", "coordinates" : [ 106.6202887, -6.1293536 ] } }
{ "_id" : ObjectId("5281c7ba2dfd7bdc64000003"), "location" : { "type" : "Point", "coordinates" : [ -86.9248483, 33.4480108 ] } }
{ "_id" : ObjectId("5281c8b82dfd7bdc64000004"), "location" : { "type" : "Point", "coordinates" : [ -74.0087126, 40.7136487 ] } }
{ "_id" : ObjectId("5281c9782dfd7bdc64000005"), "location" : { "type" : "Point", "coordinates" : [ -122.1575745, 37.4457966 ] } }
结果不正确:
[{"location":{"type":"Point","coordinates":[-122.1575745,37.4457966]},"dis":13.69288259318155},
{"location":{"type":"Point","coordinates":[-86.9248483,33.4480108]},"dis":12697164592.388557},
{"location":{"type":"Point","coordinates":[-74.0087126,40.7136487]},"dis":16328789117.58145},
{"location":{"type":"Point","coordinates":[106.6202887,-6.1293536]},"dis":55446284682.14049},
{"location":{"type":"Point","coordinates":[106.6202887,-6.1293536]},"dis":55446284682.14049}]