0
我有一個下面的代碼應該返回那些拉特,長在mongodb這是我提到的附近。但問題是,它返回所有經緯度。附近返回集合中的所有文檔
$collection->ensureIndex(array("location" => "2d"));
$a=array(24.8934,67.0281);
//print_r($a);
$distance='500';
$query = array('location' => array('$near' => array(24.8934,67.0281)));
$cursor = $collection->find($query);
try{
if ($cursor) {
echo $arr= json_encode(iterator_to_array($cursor));
// $j= json_decode($arr,false);
echo var_dump(json_decode($arr));
$j = json_decode($arr,false);
$lat= $j->{'57237036d89c45e1e3fda94e'}->location[0];
$lng= $j->{'57237036d89c45e1e3fda94e'}->location[1];
} else {
echo "{ 'status' : 'false' }";
}
它返回如下:
object(stdClass)[7]
public '572453d55addfab49090ea71' =>
object(stdClass)[6]
public '_id' =>
object(stdClass)[8]
public '$id' => string '572453d55addfab49090ea71' (length=24)
public 'location' =>
array (size=2)
0 => float 24.8615
1 => float 67.0099
public '57237036d89c45e1e3fda94e' =>
object(stdClass)[9]
public '_id' =>
object(stdClass)[10]
public '$id' => string '57237036d89c45e1e3fda94e' (length=24)
public 'location' =>
array (size=2)
0 => float 33.7715
1 => float 72.7511
相反,它應該只返回第一個文檔。