I am trying to output results using the geoNear command using PHP but what I keep getting back is one document that I can retrieve using print_r.
This is my example the search 1 works fine but search 2 doesn’t do anything.
<?php
$mongo = new Mongo();
$collection = $mongo->selectDB('users')->selectCollection('user');
$lng = -2.28;
$lat = 53.46;
$lnglat = array($lng, $lat);
$query = array('loc' => array('$near' => $lnglat));
//search 1
$cursor = $collection->find($query)->limit(10);
while($cursor->hasNext()){
$user = $cursor->getNext();
echo $user['username'] . "<br />";
};
//search 2
$cursor2 = $mongo->selectDB('users')->command(array(
'geoNear' => 'user',
'near' => $lnglat,
'spherical' => true,
'num' => 10 ));
while($cursor2->hasNext()){
$user = $cursor2->getNext();
echo $user['username'] . "<br />";
};
?>
command()doesn’t return a cursor, just an associative array. Just do: