2017-03-07 86 views
0

我的問題與處理數據庫中的數據相關gps點(在我的情況nosql DB MongoDB中)的最佳方法有關,以便僅返回最近的點。在NOSQL數據庫中搜索GPS點的最佳方式

我有我的數據庫中的用戶集合。 現在我需要創建一個新的「表」,它將用戶與gps點相關聯(用戶可以有更多的點數)。例如:

User,lat,long 
ALFA,40,50 
ALFA,30,50 
BETA,42,33 
... 

應使服務器能夠使用的功能,給定輸入的位置,返回其關聯到輸入近點的用戶列表。 例如:

function nearestUsers(lat,lon){ 
var mindif = 10000; 
var closest; 

users = getAllRecordsFromDataBase(); //query for MongoDB that returnst all records of the new table 

for (i = 0 ; i < users.length; i++){ 
    if(this.distance(lat,lon,users[i].lat,users[i].lon)>mindif) delete users[i]; 
} 

return users; 

} 

距離函數如下:

function distance(lat1, lon1, lat2, lon2) { 
    lat1 = Deg2Rad(lat1); 
    lat2 = Deg2Rad(lat2); 
    lon1 = Deg2Rad(lon1); 
    lon2 = Deg2Rad(lon2); 
    var R = 6371; 
    var x = (lon2 - lon1) * Math.cos((lat1 + lat2)/2); 
    var y = (lat2 - lat1); 
    var d = Math.sqrt(x * x + y * y) * R; 
    return d; 
} 

我怕的是,對於大數據量,這個計算策略會導致慢。哪種方法可以使算法更具可擴展性?有什麼建議麼?

考慮到這種funcionality是在Node.js中使用MongoDB在我的服務器中,我可以直接通過查詢或在我的數據庫中使用某種特殊結構來實現此功能嗎?

+0

在用戶lat上創建一個2dSphere索引,並使用地理空間查詢來查找它.https://docs.mongodb.com/manual/core/2dsphere/ –

+0

可能的重複[如何查找附近有緯度和經度在MongoDB?](http://stackoverflow.com/questions/26710271/how-can-i-find-nearby-place-with-latitude-and-longitude-in-mongodb) –

回答

相關問題