2dsphere索引查询返回所有文档

时间:2014-05-21 14:47:39

标签: mongodb 2dsphere

我在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

1 个答案:

答案 0 :(得分:2)

您正在从2.6.1版本运行mongo shell,但仍然拥有2.2.2版本的mongod版本。当您使用自制软件,macports或两者的组合时,可能会在Mac OS X安装上发生这种情况。基本上你路径中的第一个mongod不是最新版本。为了纠正我,请尝试以下方法:

  1. 通过运行&#34来找出mongo和mongod的运行位置 蒙戈"和#34;哪个mongod"。
  2. 通过运行" echo $ PATH"
  3. 来检查您的路径
  4. 找到正确的mongod文件。
  5. 更改个人资料中的路径以搜索正确的目录 mongod first。
  6. 或者,你可以在启动mongodb时使用正确的mongod的完整路径。

    或者,您可以直接从mongodb再次下载。