$near需要GeoJSON点,但提供了GeoJSON点。

3

我有一些包含类似以下地理字段的文件。

"geo" : {
    "type" : "Point",
    "coordinates" : [
        37.44609999,
        -121.88355687
    ]
},

我已创建了一个空间索引,证明如下。
db.collection.getIndexes()
[
    {
        "v" : 1,
        "key" : {
        "_id" : 1
        },
        "ns" : "db.collection",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
        "geo.coordinates" : "2dsphere"
        },
        "ns" : "db.collection",
        "name" : "geo.coordinates_2dsphere"
    }
]

然而,当我执行以下查询(直接从MongoDB手册中复制)时:
db.collection.find( { 'geo.coordinates': { $near: 
                                            { $geometry :
                                                { type : "Point", 
                                                  coordinates: [ 37.44609999, -121.88355687 ] } }, 
                                                  $maxDistance: 1000
                                         } } )

我遇到了以下错误。
error: {
    "$err" : "$near requires geojson point, given { type: \"Point\", coordinates: [ 37.44609999, -121.88355687 ] }",
    "code" : 16681
}

有人能告诉我为什么吗?(涉及IT技术)
1个回答

2

将点翻转如下:

"geo" : { "type" : "Point", "coordinates" : [ -121.88355687, 37.44609999 ] },

否则,请检查您的坐标是否正确: http://geojsonlint.com/


根据geojsonlint.com的说法,我的坐标顺序有误。但是我的文档是直接从Twitter Streaming API获取的推文。这是否意味着Twitter为其流中的每个推文创建了无效的geoJSON?此外,解决此问题的唯一方法似乎是我需要翻转到目前为止收集的每个文档(推文)中的坐标。 - Cathal Coffey
没关系,Twitter已经意识到了这个错误。现在所有的推文都有一个坐标字段,其中坐标按正确顺序指定,并且还有一个geo.coordinates字段,其中坐标是反向的。 - Cathal Coffey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接