mongodb $ geointersects不工作

时间:2016-09-20 09:05:31

标签: mongodb

我正在尝试使用$geointersects中的mongodb来查找该点所在的多边形。

php我创建了测试数据:

$collection = $db->selectCollection('countries');
$result = $collection->insertMany([
    [
        'c' => 'rus',
        'n' => 'Russia',
        'loc' => [
            "type" => "Polygon",
            "coordinates" => [
                [-109.95117, 39.77477],
                [-100.23926, 46.70974],
                [-94.41608,  37.78902],
                [-104.67541, 32.70375],
                [-109.95117, 39.77477]
            ]
       ]
    ],[
        'c' => 'can',
        'n' => 'Canada',
        'loc' => [
            "type" => "Polygon",
            "coordinates" => [
                [-100.15137, 46.83013],
                [-84.31555,  47.89965],
                [-82.39746,  37.78808],
                [-94.28467,  37.8228 ],
                [-100.15137, 46.83013]
            ]
       ]
    ]
]);

结果是:

enter image description here

然后在Robomongo我正在做一个选择:

  

db.getCollection('countries')。find({“loc”:{$ geoIntersects:   {$ geometry:{“type”:“Point”,“coordinates”:[ - 88.0123388,41.8333926   ]}}}})

enter image description here

并且没有返回任何行!这可能是个问题?

在shell中相同:

enter image description here

以下是我正在测试的多边形:

enter image description here

这是重点(41.8333926,-88.0123388)location

enter image description here

使用最新版本的mongodb:3.2.9。

1 个答案:

答案 0 :(得分:0)

答案是坐标结构不正确。我想创建一个索引,但它失败了......这就是如何找到它的。

正确数组:

"coordinates" => [[
                [-109.95117, 39.77477],
                [-100.23926, 46.70974],
                [-94.41608,  37.78902],
                [-104.67541, 32.70375],
                [-109.95117, 39.77477]
            ]]

因此,在我最初的问题中,我将点数放入应该是多边形的点。