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


Mongo php Can't canonicalize query: BadValue bad geo query'

我正在尝试使用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']
            );
        }
    }

这是我得到的错误:

致命错误:未捕获异常' mongocursoreexception ' with消息' someeserver.com:27017:无法规范化查询:BadValue坏/var/www/html/one-call/lib/somefile.php:97

MongoDB Version 2.6.1

我可以重现这个错误。我想你缺少了一个数组调用,围绕着坐标数组:

    $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)
                   )
                 )
              )
          )
       )
    );

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