MongoDB“无法找到 $geoNear 查询的索引" [英] MongoDB 'unable to find index for $geoNear query'

查看:31
本文介绍了MongoDB“无法找到 $geoNear 查询的索引"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想让一个简单的 near 查询工作.这是我的文档示例.

I'm just trying to get a simple near query working. Here's a sample of my document.

{"point": 
  {"type": "Point", 
     "coordinates": [30.443902444762696, -84.27326978424058]}, 
   "created_on": {"$date": 1398016710168}, 
   "radius": 180, 
   "user": {"$oid": "53543188eebc5c0cc416b77c"}, 
   "_id": {"$oid": "53544306eebc5c0ecac6cfba"}, 
   "expires_on": {"$date": 1399831110168}
}

和 mongod 我尝试了命令:

and with mongod I tried the command:

db.bar.find({point: {$near: [-84.26060492426588, 30.45023887165371]}});

但我收到此错误:

错误:{"$err" : "无法执行查询:错误处理查询:ns=foo.bar skip=0 Tree: GEONEAR field=point maxdist=1.79769e+308 isNearSphere=0 || First: notFirst: full path: point Sort:{} Proj:{} 规划器返回错误:无法找到 $geoNear 查询的索引",代码":17007}

error: { "$err" : "Unable to execute query: error processing query: ns=foo.bar skip=0 Tree: GEONEAR field=point maxdist=1.79769e+308 isNearSphere=0 || First: notFirst: full path: point Sort: {} Proj: {} planner returned error: unable to find index for $geoNear query", "code" : 17007 }

也许我今天的 google fu 不是那么敏锐,但我找不到任何东西.另外,我运行了 ensure index 命令.我的意图是这些是地图位置.

Maybe my google fu is not so sharp today but I couldn't find anything. Also, I ran the ensure index command. My intention is that these are map locations.

db.bar.ensureIndex({a:1});
db.bar.ensureIndex({geo:"2d"});

推荐答案

很少有问题,您在 foo 数据库的 foo 集合上创建了索引,但正在查询 bar 集合.您需要在正确的集合中.

Few problems, you created your indexes on the foo collection of the foo database, but are querying the bar collection. You need to be on the correct collection.

阅读插入的文档需要添加2dsphere"索引支持geoJson对象.此索引需要位于文档的点"元素上,因此请尝试

Reading the document you have inserted you need to add a "2dsphere" index to support the geoJson objects. This index needs to be on the "point" element of your documents, so try

db.bar.createIndex({point:"2dsphere"});

然后您可以通过为查询提供 geoJson obj 进行如下查询:

You can then query as follows by providing a geoJson obj for the query:

db.bar.find(
   { point :
       { $near :
          {
            $geometry : {
               type : "Point" ,
               coordinates : [-84.27326978424058, 30.443902444762696] },
            $maxDistance : 1
          }
       }
    }
)

这篇关于MongoDB“无法找到 $geoNear 查询的索引"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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