我在Mac OS X上使用MongoDB 2.6.1
我有一个locations
的集合,其中包含有效的GeoJSON
字段:
db.locations.findOne({}, {"location": 1})
{
1"_id" : ObjectId("534fd2b6b13e51768cd0e9c8"),
"location" : {
"type" : "Point",
"coordinates" : [
41.311,
56.3526
]
}
}
我的数据库中有2dsphere
索引:
db.locations.getIndexes()
[
{
"v" : 1,
"key" : {
"location" : "2dsphere"
},
"ns" : "insta_locations.locations",
"name" : "location_2dsphere"
},
...
]
但是当我在没有提示的情况下查询位置时,我收到错误:
db.locations.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [41.311, 56.35]
},
$maxDistance: 1
}
}
})
产生
错误:{ " $ ERR" :"无法找到特殊索引:2d:{location:{$ near:{$ geometry:{type:\" Point \&#34 ;, coordinates:[41.311,56.35] },$ maxDistance:1.0}}}", "代码" :13038 }
另一方面,提示它返回整个集合:
db.locations.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [41.311, 56.35]
},
$maxDistance: 1
}
}
}).hint('location_2dsphere').length()
5138
有谁知道我错过了什么让它正常工作?
> version()
2.6.1
> db.version()
2.2.2
答案 0 :(得分:2)
您正在从2.6.1版本运行mongo shell,但仍然拥有2.2.2版本的mongod版本。当您使用自制软件,macports或两者的组合时,可能会在Mac OS X安装上发生这种情况。基本上你路径中的第一个mongod不是最新版本。为了纠正我,请尝试以下方法:
或者,你可以在启动mongodb时使用正确的mongod的完整路径。
或者,您可以直接从mongodb再次下载。