2
votes

I am new to mongodb, trying to fetch result from mongodb using geospatial. With this [http://jamesroberts.name/blog/2012/03/12/mongodb-geospatial-query-example-in-php-with-distance-radius-in-miles-and-km/][1] reference, I created 2d indexer on my collection.

I am getting result but not getting in sorted order of Distance. I tried $near operator also but it returns 100 documents. I want all result near by given location under given miles Can anyone please help me to get data in sorted order of distance?

$connection = new MongoClient( "mongodb://192.168.1.167" );
$dbname="mydb";
$collectionName="user";
$db = $connection->$dbname;
$collection = $db->$collectionName;

$lat="36.23304900"; 
$lon="-115.24228800"; 
$radius = 40;
$collection = $db->$collectionName;
$radiusOfEarth = 3956; //avg radius of earth in miles
$query = array('lnglat' =>array('$geoWithin' =>array('$centerSphere' =>array(array(floatval($lon), floatval($lat)), $radius/$radiusOfEarth))) ,'user_id' => "1234" ,'type'=>"my_type");

$cursor = $collection->find($query);
1

1 Answers

0
votes

Try This:

$cursor = $collection->find($query);

$cursor->sort(array('distance' => -1));