2014-05-09 36 views
2

我試圖建立一個查詢來使用php-mongo庫在mongo中搜索地理空間數據。Mongo php不能規範化查詢:BadValue錯誤的地理查詢'

對象結構

{ 
    "_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' 與 消息「someserver。 com:27017:無法canonicalize查詢:BadValue不好在/var/www/html/one-call/lib/somefile.php:97

MongoDB的版本2.6.1

回答

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

修改代碼後,我沒有收到錯誤了

+0

我原本認爲這是問題,但我想它和以前一樣得到了同樣的錯誤。我會更新代碼以反映這一點。 –

+0

您可以共享有關示例文檔模式和數據以及索引的更多數據嗎?因爲它對我來說2.6代碼運行良好,沒有錯誤。 –