MongoDB“无法找到 $geoNear 查询的索引”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23188875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB 'unable to find index for $geoNear query'
提问by frankV
I'm just trying to get a simple near
query working. Here's a sample of my document.
我只是想让一个简单的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}
}
and with mongod I tried the command:
和 mongod 我尝试了命令:
db.bar.find({point: {$near: [-84.26060492426588, 30.45023887165371]}});
but I get this error:
但我收到此错误:
error: { "$err" : "Unable to execute query: error processing query: ns=foo.bar skip=0\nTree: GEONEAR field=point maxdist=1.79769e+308 isNearSphere=0 || First: notFirst: full path: point\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query", "code" : 17007 }
error: { "$err" : "无法执行查询:错误处理查询:ns=foo.bar skip=0\nTree: GEONEAR field=point maxdist=1.79769e+308 isNearSphere=0 || First: notFirst: full path : point\n排序: {}\nProj: {}\n 规划器返回错误: 无法找到 $geoNear 查询的索引", "code" : 17007 }
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.
也许我今天的 google fu 不是那么敏锐,但我找不到任何东西。另外,我运行了 ensure index 命令。我的意图是这些是地图位置。
db.bar.ensureIndex({a:1});
db.bar.ensureIndex({geo:"2d"});
回答by daveh
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.
几乎没有问题,您在 foo 数据库的 foo 集合上创建了索引,但正在查询 bar 集合。你需要在正确的集合上。
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
阅读您插入的文档,您需要添加一个“2dsphere”索引以支持 geoJson 对象。此索引需要位于文档的“点”元素上,因此请尝试
db.bar.createIndex({point:"2dsphere"});
You can then query as follows by providing a geoJson obj for the query:
然后,您可以通过为查询提供 geoJson obj 进行如下查询:
db.bar.find(
{ point :
{ $near :
{
$geometry : {
type : "Point" ,
coordinates : [-84.27326978424058, 30.443902444762696] },
$maxDistance : 1
}
}
}
)
回答by Krish Nakum R
回答by Neil Lunn
So there seems to be a couple of things wrong here:
所以这里似乎有一些问题:
- From the data you are showing and also your query information the relevant information is contained under the field point and in GeoJSON format. Your index creation:
- 根据您显示的数据以及您的查询信息,相关信息包含在字段点下并采用 GeoJSON 格式。您的索引创建:
db.foo.createIndex({geo: "2d"})
Does not "fail" because there presently isn't a field called "geo" and the field with the data should have been in that place. If you had used "point" instead, which is the correct field, then you would have received an error telling you that this type of index is invalid for the GeoJSON data. You need a "2dsphere" index:
不会“失败”,因为目前没有名为“geo”的字段,并且包含数据的字段应该在那个地方。如果您改用“point”,这是正确的字段,那么您会收到一条错误消息,告诉您这种类型的索引对 GeoJSON 数据无效。您需要一个“2dsphere”索引:
db.points.createIndex({ "point": "2dsphere" })
- Extending the same problem, again the data is in GeoJSON format and the form of the query is that for a legacy coordinate pair. You need to change the query arguments so that no longer fails:
- 扩展相同的问题,数据再次采用 GeoJSON 格式,查询的形式是旧坐标对的形式。您需要更改查询参数,以便不再失败:
db.points.find({point: { $near: { $geometry:{ type: "Point", coordinates: [-84.26060492426588, 30.45023887165371] } } }})
See the documentation for $near
请参阅文档 $near
回答by George Katsanos
In addition to the answers above, if you've already tried to create an Index and got some syntax or field wrong, you can run
除了上面的答案之外,如果您已经尝试创建索引并且有一些语法或字段错误,您可以运行
db.<yourcollection>.dropIndexes();
To clean up all indexes and re-create them properly.
db.<yourcollection>.dropIndexes();
清理所有索引并正确重新创建它们。
Also, the index should be created on the parent of "coordinates", not on coordinates itself:
此外,索引应该在“坐标”的父级上创建,而不是在坐标本身上创建:
{
"_id": 59ac03d168eaaa14c2a57a00",
"location":{
"type":"Point",
"coordinates":[
131.6667,
57.8368
]
},
"age":53,
"username":"Brandi_Greenfelder"
}
db.<yourcollection>.createIndex({ location: '2dsphere' });
db.<yourcollection>.createIndex({ location: '2dsphere' });
Attention, there is "2d" and "2dsphere", use the second as it's the new thing.
注意,有“2d”和“2dsphere”,使用第二个,因为它是新事物。