【问题标题】:Near operator for geojson point returning error when maxdistance is used in query在查询中使用 maxdistance 时,geojson 点的 Near 运算符返回错误
【发布时间】:2017-04-15 03:55:58
【问题描述】:

我正在尝试在 mongo db 中存储一些 geojson 点。这是我定义的 mongoose 模式。

new mongoose.Schema({
        itemName:String,
        loc:{ type: [Number], index: '2dsphere'}
    });

接下来我尝试运行一个 near 运算符来找出附近的一些点。这就是我定义 near 查询的方式。

Model.where('loc').near({ center: { type:"Point",coordinates: [10,10] }, maxDistance: 20 }).exec(function(err,data){
            console.log(err);
            if (!err && data) {
                console.log("Success");
            } else {
                console.log("Error");
            }
        });

但这会返回一个错误值,如下所示:

{ [MongoError: Can't canonicalize query: BadValue geo near accept 查询 GeoJSON 点时只需一个参数。额外字段 找到:$maxDistance:20] 名称:'MongoError' }

如果我删除maxDistance,它将返回所有集合元素。

知道为什么会出现此错误吗?在此先感谢您的帮助

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query geojson


    【解决方案1】:

    Mongoose 仍在使用“geoNear”数据库命令形式。这在所有正在进行的 MongoDB 版本中都被认为已过时。

    改用标准查询表单,自 MongoDB 2.6 及更高版本以来,该表单已与标准查询引擎集成:

    Model.find({
        "loc": { 
            "$near": {
                "$geometery": {
                    "type": "Point",
                    "coordinates": [ 10,10 ],
                },
                "$maxDistance": 20
            }
        }
    },function(err,docs) {
    
        // do something here
    });
    

    它是 JavaScript,一种“动态类型语言”。对于没有用于定义和对象结构的动态构造的strict 类型语言,您不需要这些荒谬的函数助手。

    所以按照手册(所有示例都使用 JSON 表示法,JavaScript 本身可以理解)告诉你去做,你总是没问题的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      相关资源
      最近更新 更多