MongoDB“无法找到 $geoNear 查询的索引”

2024-02-03

我只是想得到一个简单的near查询工作。这是我的文档的示例。

{"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 我尝试了以下命令:

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

但我收到此错误:

错误: { "$err" : "无法执行查询: 处理查询时出错: ns=foo.bar skip=0\nTree: GEONEAR field=point maxdist=1.79769e+308 isNearSphere=0 || First: notFirst: 完整路径: point\ nSort: {}\nProj: {}\n 规划器返回错误: 无法找到 $geoNear 查询的索引", “代码”:17007 }

也许我的谷歌今天不太敏锐,但我找不到任何东西。另外,我运行了 Ensure Index 命令。我的意图是这些是地图位置。

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

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对象 http://docs.mongodb.org/manual/core/2dsphere/。该索引需要位于文档的“点”元素上,因此请尝试

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

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

db.bar.find(
   { point :
       { $near :
          {
            $geometry : {
               type : "Point" ,
               coordinates : [-84.27326978424058, 30.443902444762696] },
            $maxDistance : 1
          }
       }
    }
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MongoDB“无法找到 $geoNear 查询的索引” 的相关文章

随机推荐