MongoDB';找不到$geoNear查询的索引';
我只是想让一个简单的MongoDB';找不到$geoNear查询的索引';,mongodb,geospatial,mongodb-query,Mongodb,Geospatial,Mongodb Query,我只是想让一个简单的查询工作起来。这是我的文件样本 {"point": {"type": "Point", "coordinates": [30.443902444762696, -84.27326978424058]}, "created_on": {"$date": 1398016710168}, "radius": 180, "user": {"$oid": "53543188eebc5c0cc416b77c"}, "_id": {"$oi
查询工作起来。这是我的文件样本
{"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:full path:point\nOrst:{}\nProj:{}\n planner返回错误:找不到$GEONEAR查询的索引”,
“代码”:17007
}
也许我的谷歌fu今天不那么锐利,但我什么也找不到。此外,我还运行了确保索引命令。我的意图是这些都是地图位置
db.bar.ensureIndex({a:1});
db.bar.ensureIndex({geo:"2d"});
没有什么问题,您在foo数据库的foo集合上创建了索引,但正在查询bar集合。您需要使用正确的集合
阅读插入的文档时,需要添加“2dsphere”索引。此索引需要位于文档的“point”元素上,因此请尝试
db.bar.createIndex({point:"2dsphere"});
然后,您可以通过为查询提供geoJson obj进行如下查询:
db.bar.find(
{ point :
{ $near :
{
$geometry : {
type : "Point" ,
coordinates : [-84.27326978424058, 30.443902444762696] },
$maxDistance : 1
}
}
}
)
所以这里似乎有两件事不对:
- 根据所显示的数据和查询信息,相关信息以GeoJSON格式包含在字段点下。您的索引创建:
- 扩展同样的问题,数据也是GeoJSON格式,查询的形式是遗留坐标对。您需要更改查询参数,以便不再失败:
db.points.find({point:{
$near:{
$geometry:{
键入:“点”,
座标:[-84.26060492426588,30.45023887165371]
}
}
}})
有关详细信息,请参阅文档
这对我来说解决了同样的问题
其中prod是我的集合名称,location是存储地理位置(GeoPoint)的列的名称
除了上面的答案外,还可以找到一些关于这方面的讨论,如果您已经尝试创建索引,但某些语法或字段出现错误,则可以运行
db..dropIndexes()代码>
清理所有索引并正确地重新创建它们
此外,索引应在“坐标”的父项上创建,而不是在坐标本身上创建:
{
"_id": 59ac03d168eaaa14c2a57a00",
"location":{
"type":"Point",
"coordinates":[
131.6667,
57.8368
]
},
"age":53,
"username":"Brandi_Greenfelder"
}
db..createIndex({location:'2dsphere'})代码>
注意,有“2d”和“2d球体”,使用第二个,因为它是新事物 如果您使用猫鼬连接,这将是正确的答案:
db.collections.<yourcollection>.createIndex({ location : "2dsphere" })
很好,使用:db..ensureIndex({point:“2dsphere”})解决了我的问题
注意sine3.0.0
ensureIndex
是createIndex
的别名(答案进行了相应编辑)在compass中对此有何想法:geoNear命令失败:{operationTime:Timestamp(1536208908,1),ok:0.0,errmsg:“2dsphere索引必须具有球形:true”,代码:17301,代码名:“Location17301”,$clusterTime:{clusterTime:Timestamp(1536208908,1),
用于{“近”:{“类型”:“点”,“坐标”:[-73.99279,40.719296]},“距离字段”:“距离”}
@deadManN在距离字段:“距离”
之后添加球形:true
。这对我很有效。谢谢你的帮助“location是存储地理位置(地理点)的列的名称”
db.points.find({point: {
$near: {
$geometry:{
type: "Point",
coordinates: [-84.26060492426588, 30.45023887165371]
}
}
}})
db.prod.createIndex({ "location": "2d" })
{
"_id": 59ac03d168eaaa14c2a57a00",
"location":{
"type":"Point",
"coordinates":[
131.6667,
57.8368
]
},
"age":53,
"username":"Brandi_Greenfelder"
}
db.collections.<yourcollection>.createIndex({ location : "2dsphere" })
console.log(db)