2017-10-10 79 views
0

我收到錯誤「當我們在經度和緯度上使用maxDistance查找記錄基礎時,無法在節點快遞中使用$ near與String錯誤」。

下面是代碼,我使用: -

1架構代碼: -

var mongoose = require('mongoose'), 
Schema = mongoose.Schema; 


var dtSchema = new Schema({ 
    name:String, 
    date: {type: Date, default: Date.now}, 
    location: { type: String, coordinates: [Number] }, 
    category:String 
}); 

//dtSchema.index({location: '2dsphere'}); 

module.exports = mongoose.model('places', dtSchema); 

2 server.js代碼提取記錄

app.get('/location/:lon/:lat', function(req, res) { 
    console.log(req.params.lon); 
    console.log(req.params.lat); 
    location.find({location: 
     { $near: 
      { 
      $geometry: { type: "Point", coordinates: [req.params.lon, req.params.lat ] }, 
      $maxDistance: 5000 
      } 
     }}, function(err, places) { 

     if(!err){ 

      res.send('({"records":' + JSON.stringify(places) + '});'); 
     } 
     else{ 
      console.log(err); 
      res.send("error coming") 
     } 
    }); 


}); 

參數發送URL

http://localhost:4700/location/-73.9667/40.78

當我們執行代碼並打在URL上方時。我發現錯誤「無法使用$附近與字符串錯誤的節點快遞」

回答

0

db.location.aggregate([{「$ geoNear」:{「near」:{「type」:「Point」,「coordinates 「:[72.4468035,23.032069]},」maxDistance「:500,」spherical「:true,」distanceField「:」distance「}}])

+0

下面提到查詢是在mongo shell中運行,但不能與mongoose一起工作。幫助我簡化問題 db.location.find({location:{$ nearSphere:{$ geometry:{type:「Point」,coordinates:[-73.93414657,40.82302903]},$ maxDistance:500}}} ) – pkshakya

+0

即時通訊不知道爲什麼這個查詢不工作在貓鼬 –

+0

你可以給我例子哪些商店在你的位置集合?在我的查詢 –