2016-01-25 35 views
0

我在用Express + Mongoose構建的應用程序中查詢MongoDB集合。我正在尋找距離地理位置一定距離內的文件。結果顯示的文件遠在我指定的最大距離之外。如何在Mongoose中正確指定geoNear查詢中的最大距離?

此型號爲我查詢集合:

LocationSchema = new mongoose.Schema({ 
    id: { 
     type: String, 
     unique: true 
    }, 
    ref: String, 
    name: String, 
    loc: { 
     type: [Number], // [<longitude>, <latitude>] 
     index: '2d'  // create the geospatial index 
    } 
}); 

這是查詢DB(通過貓鼬)的代碼:

var queryOptions = { 
    maxDistance: 30, 
    // maxDistance : 30/6371, // <-- (this doesn't work) 
    distanceMultiplier: 6371, // tell mongo how many radians go into one kilometer. 
    spherical: true, 
    limit: 60 
}; 

location.geoNear([latitude, longitude], queryOptions, processResults); 

在這個截圖中,我指定的COORDS在查詢中是都柏林的中心。如您所見,結果遠遠超出我指定的最大距離30公里。

我哪裏出錯了?

enter image description here

回答

0

的問題是在這行代碼:

location.geoNear([latitude, longitude], queryOptions, processResults); 

應該先經度,緯度,然後!改變它解決了這個問題:

location.geoNear([longitude, latitude], queryOptions, processResults); 

簡單。