本文介绍了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]}});

但我收到此错误:

也许我的Google Fu今天不是很敏锐,但是我什么也找不到.另外,我运行了保证索引命令.我的意图是这些是地图位置.

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 need to be on the correct collection.

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

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查询的索引'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:54
查看更多