Node.js 如何使用mongoDB和mongoose计算两个用户之间的距离
我有一个用户模式(mongoose),它有一个字段“location”-它由一个数组[经度,纬度] 现在,我希望使用地理空间服务查询数据库,以查找从user1到user2的距离Node.js 如何使用mongoDB和mongoose计算两个用户之间的距离,node.js,mongodb,mongoose,geospatial,Node.js,Mongodb,Mongoose,Geospatial,我有一个用户模式(mongoose),它有一个字段“location”-它由一个数组[经度,纬度] 现在,我希望使用地理空间服务查询数据库,以查找从user1到user2的距离 我该怎么做呢?这应该可以让你开始了 您需要在架构中定义一个属性: 'location' : { type: { type: String }, coordinates: [] }, 并将属性索引为2dsphere yourSchema.index({'location' : "2dsphere"})
我该怎么做呢?这应该可以让你开始了 您需要在架构中定义一个属性:
'location' : {
type: { type: String },
coordinates: []
},
并将属性索引为2dsphere
yourSchema.index({'location' : "2dsphere"})
然后,您可以执行以下操作:
//Model.geoNear(GeoJSON, options, [callback]) need a GeoJSON point to search in radius
var point = { type : "Point", coordinates : [data.coordinates.long, data.coordinates.lat] };
YourModel.geoNear(point, { maxDistance : data.distance /coordinatesUtils.earthRadius, spherical : true }, function(err, results, stats) {
res.status(200);
res.json(results);
});
但有几件事需要注意:
要使球形查询运算符正常工作,必须转换
将距离转换为弧度,并将弧度转换为距离单位
由您的应用程序使用
要转换:距离到弧度:将距离除以半径
球体(如地球)的单位与距离相同
测量
弧度到距离:将弧度测量值乘以距离的半径
要转换的单位系统中的球体(例如地球)
距离
地球的半径约为3959英里或6371英里
公里
取自
在mongoose中有一个将坐标从GeoJSON中剥离出来,并将它们像传统的一对一样发送到mongo,这使得near操作以弧度而不是米为单位工作
现在可能已经修好了,但我不确定
您还可以在文档中阅读
您可以阅读有关GeoJson的内容这应该可以让您开始学习 您需要在架构中定义一个属性:
'location' : {
type: { type: String },
coordinates: []
},
并将属性索引为2dsphere
yourSchema.index({'location' : "2dsphere"})
然后,您可以执行以下操作:
//Model.geoNear(GeoJSON, options, [callback]) need a GeoJSON point to search in radius
var point = { type : "Point", coordinates : [data.coordinates.long, data.coordinates.lat] };
YourModel.geoNear(point, { maxDistance : data.distance /coordinatesUtils.earthRadius, spherical : true }, function(err, results, stats) {
res.status(200);
res.json(results);
});
但有几件事需要注意:
要使球形查询运算符正常工作,必须转换
将距离转换为弧度,并将弧度转换为距离单位
由您的应用程序使用
要转换:距离到弧度:将距离除以半径
球体(如地球)的单位与距离相同
测量
弧度到距离:将弧度测量值乘以距离的半径
要转换的单位系统中的球体(例如地球)
距离
地球的半径约为3959英里或6371英里
公里
取自
在mongoose中有一个将坐标从GeoJSON中剥离出来,并将它们像传统的一对一样发送到mongo,这使得near操作以弧度而不是米为单位工作
现在可能已经修好了,但我不确定
您还可以在文档中阅读
您可以阅读有关GeoJson的内容这应该可以让您开始学习 您需要在架构中定义一个属性:
'location' : {
type: { type: String },
coordinates: []
},
并将属性索引为2dsphere
yourSchema.index({'location' : "2dsphere"})
然后,您可以执行以下操作:
//Model.geoNear(GeoJSON, options, [callback]) need a GeoJSON point to search in radius
var point = { type : "Point", coordinates : [data.coordinates.long, data.coordinates.lat] };
YourModel.geoNear(point, { maxDistance : data.distance /coordinatesUtils.earthRadius, spherical : true }, function(err, results, stats) {
res.status(200);
res.json(results);
});
但有几件事需要注意:
要使球形查询运算符正常工作,必须转换
将距离转换为弧度,并将弧度转换为距离单位
由您的应用程序使用
要转换:距离到弧度:将距离除以半径
球体(如地球)的单位与距离相同
测量
弧度到距离:将弧度测量值乘以距离的半径
要转换的单位系统中的球体(例如地球)
距离
地球的半径约为3959英里或6371英里
公里
取自
在mongoose中有一个将坐标从GeoJSON中剥离出来,并将它们像传统的一对一样发送到mongo,这使得near操作以弧度而不是米为单位工作
现在可能已经修好了,但我不确定
您还可以在文档中阅读
您可以阅读有关GeoJson的内容这应该可以让您开始学习 您需要在架构中定义一个属性:
'location' : {
type: { type: String },
coordinates: []
},
并将属性索引为2dsphere
yourSchema.index({'location' : "2dsphere"})
然后,您可以执行以下操作:
//Model.geoNear(GeoJSON, options, [callback]) need a GeoJSON point to search in radius
var point = { type : "Point", coordinates : [data.coordinates.long, data.coordinates.lat] };
YourModel.geoNear(point, { maxDistance : data.distance /coordinatesUtils.earthRadius, spherical : true }, function(err, results, stats) {
res.status(200);
res.json(results);
});
但有几件事需要注意:
要使球形查询运算符正常工作,必须转换
将距离转换为弧度,并将弧度转换为距离单位
由您的应用程序使用
要转换:距离到弧度:将距离除以半径
球体(如地球)的单位与距离相同
测量
弧度到距离:将弧度测量值乘以距离的半径
要转换的单位系统中的球体(例如地球)
距离
地球的半径约为3959英里或6371英里
公里
取自
在mongoose中有一个将坐标从GeoJSON中剥离出来,并将它们像传统的一对一样发送到mongo,这使得near操作以弧度而不是米为单位工作
现在可能已经修好了,但我不确定
您还可以在文档中阅读
您可以阅读GeoJson