2015-01-09 119 views
1

我可以让我的元素,用下面的代码:

$lines = $loc->find(
      array("loc" => 
       array('$near' => 
        array('$geometry' => 
         array('type' => 'Point', 'coordinates' => 
          array(floatval($longitude), floatval($latitude)) 
         ), 

         '$maxDistance' => 10000 //meters 
        ) 
       ) 
      ) 
     ); 

这是结果:

Array 
(
    [_id] => MongoId Object 
     (
      [$id] => 5490003c815289663d8bbd95 
     ) 

    [name] => My adress 
    [loc] => Array 
     (
      [type] => Point 
      [coordinates] => Array 
       (
        [0] => 5.050948 
        [1] => 45.040419 
       ) 

     ) 

) 

但现在,我需要从我给点获得的距离。

可能吗?

编辑:

这是用来插入DATAS代码:

$loc = $client->localisation->localisation;  
$loc->ensureIndex(array("loc" => "2dsphere")); 

$loc->insert(
     array(
      "idobject" => $myValue 
      "name" => $myName 
      "loc" => array("type" => "Point", "coordinates" => array($longitude, $latitude)) 
     )); 

回答

1

$near运营商不与返回的结果一起返回的距离。它只是在回应中订购文件。

在PHP驱动程序下,最简单的形式是使用$geoNear聚合管道阶段。这使您可以投影领域中的效果,甚至在其他流水线阶段使用它:

$loc->aggregate(
    array(
     array( 
      '$geoNear' => array(
       'near' => array(
        'type' => 'Point', 
        'coordinates' =>array(floatval($longitude), floatval($latitude)) 
       ), 
       'maxDistance' => 1000, 
       'distanceField' => 'distance', 
       'spherical' => true     
      ) 
     ) 
    ) 
); 

有是geoNear数据库命令的形式为好,但直接指挥的结果往往不是那样好。

+0

谢谢,但我得到这个错误: 例外:geoNear命令失败:{确定:0.0,ERRMSG:“不能让查询亚军”} – bahamut100

+0

@ bahamut100你可能对集合多个地理空间索引?请注意,这些表单不指定字段,因为他们期望只有1个索引存在 –

+0

>我编辑了我的帖子以添加我用于插入数据的方式 – bahamut100