2015-01-04 64 views
4

我使用的MongoDB到GPS位置數據存儲爲此以GeoJSON文件的MongoDB geoNear命令結果距離

{ 「類型」: 「點」, 「座標」:[33.313183,44.465632]}

{ 「類型」: 「點」, 「座標」:33.2926487,44.4159651]}

更新:我也保證2dspher指數喜歡跟着

db.test.ensureIndex({ loc : "2dsphere" }) 

我的座標,從谷歌地圖 現在,如果我搜索使用此命令我不能轉換的距離的結果公里

db.runCommand({geoNear: 'test', spherical: true, near: {type: "Point" , coordinates: [33.2926487, 44.4159651]}}) 

結果如下

"results" : [ 
    { 
     "dis" : 0.0033427770982422957, 
     "obj" : { 
      "_id" : ObjectId("54a96f348fa05fcaed637a22"), 
      "loc" : { 
       "type" : "Point", 
       "coordinates" : [ 
        33.2926487, 
        44.4159651 
       ] 
      } 
     } 
    }, 
    { 
     "dis" : 5764.7060911604085, 
     "obj" : { 
      "_id" : ObjectId("54a96f4c8fa05fcaed637a23"), 
      "loc" : { 
       "type" : "Point", 
       "coordinates" : [ 
        33.313183, 
        44.465632 
       ] 
      } 
     } 
    } 
] 

的第一個結果預計將爲零,因爲它同點我想從源=目標座標得到距離,但仍然有價值。

第二個值我不能轉換成公里,在Google距離計算器中大約是5.26KM。

回答

6

距離以米爲單位返回。要轉換爲公里,由1000分可以有MongoDB的做到這一點使用distanceMultiplier選項:

db.runCommand({ "geoNear" : "test", "spherical" : true, "distanceMultiplier" : 0.001, "near" : { "type" : "Point" , "coordinates" : [33.2926487, 44.4159651] } }) 
+0

這似乎是最接近答案的實際距離,但geoNear文檔中的提到的距離在輻射如果我想轉換,我應該乘以地球的地球半徑。你怎麼看? –

+1

這裏沒有弧度。地球上兩點之間的弧度距離不能超過2 * pi。使用傳統座標對時會返回弧度。 – wdberkeley

0

您是否將該字段編入索引2dsphere。這可以解釋這種差異,因爲您查詢爲sphere: true

+0

是的,我已經添加使用 db.test.ensureIndex({LOC:「2dsphere」})2dsphere指數 –

4

當我繼續我的研究過這個問題,我發現這個 有兩種類型的存儲類型爲點

遺產點,因爲該格式

db.test2.insert({location: [33.313183, 44.465632]}) 

和GeoJSON的點,因爲該格式

db.test.insert({location: {"type" : "Point", "coordinates" : [33.313183, 44.465632]}}) 

,如果我們使用的是傳統的點,結果將是弧度,所以我們將它使用此語句

db.runCommand({geoNear: 'test2', spherical: true, near: [33.2926487, 44.4159651], distanceMultiplier: 6371}) 

轉換成公里,如果我們使用以GeoJSON點的結果將是米,所以我們將它轉​​換使用這種說法,因爲這回答了這個問題

db.runCommand({geoNear: 'test', spherical: true, near: {type: "Point" , coordinates: [33.2926487, 44.4159651]}, distanceMultiplier: 0.001}) 

solution in case of GeoJSON point format

for more details check this link, a bug report to MongoDB that explains it all.

0123公里