Mongo php不能规范化查询:BadValue bad geo query'

时间:2014-05-09 16:39:31

标签: php mongodb geospatial

我正在尝试使用php-mongo库构建一个在mongo中搜索地理空间数据的查询。

对象结构

    {
    "_id" : ObjectId("536ecef59bdc8977f1e9c06f"),
    "location" : {
        "type" : "Point",
        "coordinates" : [
            [
                -74.220408,
                40.727703
            ]
        ]
    },
    "bounds" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    -74.221403,
                    40.728457
                ],
                [
                    -74.219413,
                    40.728457
                ],
                [
                    -74.219413,
                    40.726949
                ],
                [
                    -74.221403,
                    40.726949
                ],
                [
                    -74.221403,
                    40.728457
                ]
            ]
        ]
    },
    "tile" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    -74.220498,
                    40.727772
                ],
                [
                    -74.220318,
                    40.727772
                ],
                [
                    -74.220318,
                    40.727634
                ],
                [
                    -74.220498,
                    40.727634
                ],
                [
                    -74.220498,
                    40.727772
                ]
            ]
        ]
    },
    "status" : "2",
    "dev" : "0"
}

索引

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "atlantic.Analyzer_Marks"
    },
    {
        "v" : 1,
        "key" : {
            "location.coordinates" : "2d"
        },
        "name" : "location.coordinates_2d",
        "ns" : "atlantic.Analyzer_Marks"
    },
    {
        "v" : 1,
        "key" : {
            "bounds" : "2dsphere"
        },
        "name" : "bounds_2dsphere",
        "ns" : "atlantic.Analyzer_Marks",
        "2dsphereIndexVersion" : 2
    },
    {
        "v" : 1,
        "key" : {
            "tile" : "2dsphere"
        },
        "name" : "tile_2dsphere",
        "ns" : "atlantic.Analyzer_Marks",
        "2dsphereIndexVersion" : 2
    }
]


    $mongo = new MongoClient("mongodb://someserver.com:27017");
    $marks = $mongo->selectDB('atlantic');
        $q = array('bounds' => array(
          '$geoWithin' => array(
              '$geometry' => array(
                  'type' => 'Polygon',
            'coordinates' => array(array(
                    array(40.125246,-74.327963),
                    array(40.125246,-74.325989),
                    array(40.123738,-74.325989),
                    array(40.123738,-74.327963),
                    array(40.125246,-74.327963)
            ))
              )
          )
       )
    );
$marks = new MongoCollection($marks,'Analyzer_Marks');
    $marks = $marks->find($q);
    //var_dump($marks);
    $results = array();
    if(!empty($marks)){

        foreach($marks as $mark) {
            $results[] = array(
                "location" => $mark['location'],
                "tile" => $mark['tile'],
                "status" => $mark['status']
            );
        }
    }

这是我得到的错误:

  

致命错误:未捕获的异常' MongoCursorException'同   message' someserver.com:27017:无法规范查询:BadValue不好   地理查询'在/var/www/html/one-call/lib/somefile.php:97

MongoDB版本2.6.1

1 个答案:

答案 0 :(得分:0)

我可以重现那个错误。我认为你缺少一个围绕坐标数组的数组调用:

    $mongo = new MongoClient("mongodb://someserver.com:27017");
    $marks = $mongo->selectDB('atlantic');
    $q = array('bounds' => array(
           '$geoWithin' => array(
             '$geometry' => array(
               'type' => 'Polygon',
                 'coordinates' => array(
                   array(
                     array(40.125246,-74.327963),
                     array(40.125246,-74.325989),
                     array(40.123738,-74.325989),
                     array(40.123738,-74.327963),
                     array(40.125246,-74.327963)
                   )
                 )
              )
          )
       )
    );

修改代码后,我不再收到该错误