2017-07-17 42 views
0

我需要計算一組點之間的總距離。他們都有一個時間戳讓他們按正確的順序。我只能使用MongoDB。如何計算使用MongoDB的總距離

樣品採集:

{ 
    "_id" : ObjectId("596cd354241aa3174056fb98"), 
    "spelerID" : 1, 
    "timestamp" : ISODate("2017-02-01T19:00:00.000Z"), 
    "coordinates" : { 
     "type" : "Point", 
     "coordinates" : [ 
      4.29870386367084, 
      50.8357637566422 
     ] 
    } 
} 

/* 2 */ 
{ 
    "_id" : ObjectId("596cd354241aa3174056fbb4"), 
    "spelerID" : 1, 
    "timestamp" : ISODate("2017-02-01T19:00:01.000Z"), 
    "coordinates" : { 
     "type" : "Point", 
     "coordinates" : [ 
      4.29868458167181, 
      50.8357575419868 
     ] 
    } 
} 

/* 3 */ 
{ 
    "_id" : ObjectId("596cd354241aa3174056fbce"), 
    "spelerID" : 1, 
    "timestamp" : ISODate("2017-02-01T19:00:02.000Z"), 
    "coordinates" : { 
     "type" : "Point", 
     "coordinates" : [ 
      4.29867536067721, 
      50.8357376028214 
     ] 
    } 
} 

我不知道如何計算正確的順序在這些點之間的距離。

回答

1

MongoDB可以通過使用$ near或$ geoNear來表現兩點之間的距離。

按照距離指定點(從最近到最遠)的順序返回文檔。 geoNear需要一個地理空間索引。 https://docs.mongodb.com/manual/reference/command/geoNear/

在你的情況下,我不認爲這將解決問題,因爲你想在多個點之間執行多個距離計算。

通過使用MapReduce可以指示MongoDB創建非常複雜的算法。即使我不擅長GIS應用程序,也可以嘗試創建一個帶有函數的MapReduce例程,該函數可根據需要計算多個點之間的距離。

MongoDB - MapReduce

這個鏈接可以幫助你寫一個JS函數在地圖中使用減少: Calculate distance between two latitude-longitude points? (Haversine formula)

祝您好運!