Mongodb 查询2dsphere索引以获得按“最接近的第一个”和“最大距离”排序的结果的正确方法是什么?

Mongodb 查询2dsphere索引以获得按“最接近的第一个”和“最大距离”排序的结果的正确方法是什么?,mongodb,indexing,geospatial,Mongodb,Indexing,Geospatial,在第二行“版本…”之后不久的奇怪的“找不到特殊索引…”实际上是输出。。。我不知道为什么 我试图弄明白为什么我会得到这样一个错误,即它试图找到一个二维索引,而位置字段上已经存在一个二维球体索引。。。感谢所有的帮助 > version() version: 2.0.4 can't find special index: 2d for: { location: { $near: { $geometry: { type: "Point", coordinates: [ 0.1, 0.1 ] } }

在第二行“版本…”之后不久的奇怪的“找不到特殊索引…”实际上是输出。。。我不知道为什么

我试图弄明白为什么我会得到这样一个错误,即它试图找到一个二维索引,而位置字段上已经存在一个二维球体索引。。。感谢所有的帮助

> version()
version: 2.0.4
can't find special index: 2d for: { location: { $near: { $geometry: { type: "Point", coordinates: [ 0.1, 0.1 ] } }, $maxDistance: 5000.0 } }
> version()
version: 2.0.4
can't find special index: 2d for: { location: { $near: { $geometry: { type: "Point", coordinates: [ 0.1, 0.1 ] } }, $maxDistance: 5000.0 } }
> db.syncsnapshotsphere.find()
{ "_id" : "naga", "location" : { "type" : "Point", "coordinates" : [ 0.1, 0.1 ] }, "timestamp" : 1375778757.568005 }
{ "_id" : "nagb", "location" : { "type" : "Point", "coordinates" : [ 0.2, 0.2 ] }, "timestamp" : 1375778792.552187 }
{ "_id" : "nagc", "location" : { "type" : "Point", "coordinates" : [ 0.3, 0.3 ] }, "timestamp" : 1375778803.047879 }
{ "_id" : "nagd", "location" : { "type" : "Point", "coordinates" : [ 0.4, 0.4 ] }, "timestamp" : 1375778814.088595 }
> db.syncsnapshotsphere.find({location: {$near: {$geometry: {type: "Point", coordinates: [0.1,0.1]}}, $maxDistance: 5000}})
error: {
    "$err" : "can't find special index: 2d for: { location: { $near: { $geometry: { type: \"Point\", coordinates: [ 0.1, 0.1 ] } }, $maxDistance: 5000.0 } }",
    "code" : 13038
}

2dsphere
是MongoDB 2.4中引入的索引类型,您使用的是2.0.4。因为使用2.0.x进行地理查询,MongoDB需要一个
2d
索引。请将MongoDB升级到2.4.x,这样就可以了。

您可以试试
db.syncsnapshotsphere.find({location:{$near:{$geometry:{$type:{type:'Point],座标:[[0.1,0.1]},$maxDistance:5000})
?@randunel双括号没有帮助。。。复制粘贴您的查询。。。同样的错误:(…但是谢谢。我在mongoengine的文档中看到了您刚才所说的内容…但是我无法理解现有的2dsphere索引…是不是该版本中存在索引,但仍然不支持使用这些运算符进行查询?当然,然后我将升级…您可以创建任何类型的索引:
ensureIndex({'foo':'boogiewoogie'})
> db.syncsnapshotsphere.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "logging_stuff.syncsnapshotsphere",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "location" : "2dsphere"
        },
        "ns" : "logging_stuff.syncsnapshotsphere",
        "name" : "location_2dsphere",
        "background" : false,
        "dropDups" : false
    }
]
>