sir*_*raj 14 geospatial mongodb mongodb-query
当我在MongoDB上触发此查询时,我将获得距离指定坐标500英里附近的所有位置.但我想知道指定坐标和结果位置之间的确切距离.
db.new_stores.find({ "geometry": { $nearSphere: { $geometry: { type: "Point", coordinates: [ -81.093699, 32.074673 ] }, $maxDistance: 500 * 3963 } } } ).pretty()
Run Code Online (Sandbox Code Playgroud)
我的输出看起来像:
{
"_id" : ObjectId("565172058bc200b0db0f75b1"),
"type" : "Feature",
"geometry" : {
"type" : "Point",
"coordinates" : [
-80.148826,
25.941116
]
},
"properties" : {
"Name" : "Anthony's Coal Fired Pizza",
"Address" : "17901 Biscayne Blvd, Aventura, FL"
}
}
Run Code Online (Sandbox Code Playgroud)
我也想知道这个地方与指定坐标的距离.我在几何上创建了2dsphere索引.
Bla*_*ven 21
您可以使用$geoNear
聚合管道阶段生成与查询点的距离:
db.new_stores.aggregate([
{ "$geoNear": {
"near": {
"type": "Point",
"coordinates": [ -81.093699, 32.074673 ]
},
"maxDistance": 500 * 1609,
"spherical": true,
"distanceField": "distance",
"distanceMultiplier": 0.000621371
}}
]).pretty()
Run Code Online (Sandbox Code Playgroud)
这允许您指定"distanceField"
哪个将在输出文档中生成包含与查询点的距离的另一个字段.您还可以根据"distanceMultiplier"
需要将任何转换应用于输出距离(即米到英里,并注意所有GeoJSON距离以米为单位返回)
还有geoNear
带有类似选项的命令,但它当然不会将光标作为输出返回.
MongoDB 提供了一个$geoNear聚合器,用于使用 GeoJson 坐标计算集合中文档的距离。
让我们通过一个简单的例子来理解它。考虑一个简单的集合店
1. 创建收藏
db.createCollection('shops')
Run Code Online (Sandbox Code Playgroud)
2. 在商店集合中插入文档
db.shops.insert({name:"Galaxy store",address:{type:"Point",coordinates:[28.442894,77.341299]}})
db.shops.insert({name:"A2Z store",address:{type:"Point",coordinates:[28.412894,77.311299]}})
db.shops.insert({name:"Mica store",address:{type:"Point",coordinates:[28.422894,77.342299]}})
db.shops.insert({name:"Full Stack developer",address:{type:"Point",coordinates:[28.433894,77.334299]}})
Run Code Online (Sandbox Code Playgroud)
3. 在“地址”字段上创建 GeoIndex
db.shops.createIndex({address: "2dsphere" } )
Run Code Online (Sandbox Code Playgroud)
4. 现在使用$geoNear聚合器 来查找具有距离的文档。
db.shops.aggregate([{$geoNear:{near:{type:"Point",coordinates:[28.411134,77.331801]},distanceField: "shopDistance",$maxDistance:150000,spherical: true}}]).pretty()
Run Code Online (Sandbox Code Playgroud)
这里的坐标:[28.411134,77.331801]是从中获取文档的中心位置或所需位置。
distanceField:"shopDistance", $geoNear Aggregator 返回 shopDistance 作为结果中的字段。
结果:
{ "_id" : ObjectId("5ef047a4715e6ae00d0893ca"), "name" : "Full Stack developer", "address" : { "type" : "Point", "coordinates" : [ 28.433894, 77.334299 ] }, "shopDistance" : 621.2848190449148 }
{ "_id" : ObjectId("5ef0479e715e6ae00d0893c9"), "name" : "Mica store", "address" : { "type" : "Point", "coordinates" : [ 28.422894, 77.342299 ] }, "shopDistance" : 1203.3456146763526 }
{ "_id" : ObjectId("5ef0478a715e6ae00d0893c7"), "name" : "Galaxy store", "address" : { "type" : "Point", "coordinates" : [ 28.442894, 77.341299 ] }, "shopDistance" : 1310.9612119555288 }
{ "_id" : ObjectId("5ef04792715e6ae00d0893c8"), "name" : "A2Z store", "address" : { "type" : "Point", "coordinates" : [ 28.412894, 77.311299 ] }, "shopDistance" : 2282.6640175038788 }
Run Code Online (Sandbox Code Playgroud)
这里的商店距离将以米为单位。
归档时间: |
|
查看次数: |
11300 次 |
最近记录: |