在node express中不能使用$ near和String错误

时间:2017-10-10 06:42:46

标签: node.js mongodb express

我收到错误" 在节点快递中无法使用$ near错误的字符串"当我们使用maxDistance找到经度和纬度的记录时。

以下是代码,我正在使用: -

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")
        }
    }); 


 }); 

参数发送网址

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

当我们执行代码并点击上面的URL时。我发现错误"在节点快递中不能使用$ near和String错误"

2 个答案:

答案 0 :(得分:1)

前段时间我遇到了同样的问题。错误出现在架构声明中。

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

答案 1 :(得分:0)

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