在查询中使用maxdistance时,geojson点返回错误的Near运算符 [英] Near operator for geojson point returning error when maxdistance is used in query

查看:241
本文介绍了在查询中使用maxdistance时,geojson点返回错误的Near运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to store some geojson points in mongo db.Here is the mongoose schema I have defined.

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

接下来,我试图运行一个Near运算符以找出附近的一些点.这就是我定义Near查询的方式.

Next I am trying to run a near operator to find out some points nearby.This is how I have defined the near query.

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

但这将返回错误值,如下所示:

But this is returning an error value as follows:

{[MongoError:无法规范化查询:BadValue geo附近接受 查询GeoJSON点时只有一个参数.额外领域 找到:$ maxDistance:20]名称:'MongoError'}

{ [MongoError: Can't canonicalize query: BadValue geo near accepts just one argu ment when querying for a GeoJSON point. Extra field found: $maxDistance: 20] name : 'MongoError' }

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

If i remove the maxDistance it is returning all the collection elements.

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

Any idea why this error is coming?Thanks in advance for any help

推荐答案

猫鼬仍在使用'geoNear'数据库命令格式.在所有正在进行的MongoDB版本中,这已经过时了.

Mongoose is still using the 'geoNear' database command form. This is considered obsolete in all ongoing versions of MongoDB.

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

Use the standard query form instead, which has been integrated with the standard query engine since MongoDB 2.6 and greater versions:

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

    // do something here
});

它是JavaScript,一种动态类型化的语言".您不需要strict类型语言所需要的这些荒谬的函数帮助器,而无需定义和对象结构的动态构造.

It's JavaScript, a "dynamically typed language". You don't need these ridiculous function helpers that are needed for strict typed languages with no dynamic constructs for defining and Object structure.

请按照手册(所有示例均采用JSON表示法,JavaScript可以理解)执行告诉您的操作,并且您总是会很好的.

So do what the manual (which all examples are in JSON notation, which JavaScript natively understands) tells you to do and you are always fine.

这篇关于在查询中使用maxdistance时,geojson点返回错误的Near运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆