The lastes version of DoctrineODM has the geoNear
method: See the code
However, bear in mind that geoNear
is truly an hack made for Foursquare by the MongoDB team. It returns only the first 100 results ordered by distance (and you can't paginate)
Use $near instead: $near Doc for Mongo 2.2
$query->field('coordinates')
->equals(array('$near' => array($lat, $lon)
, '$maxDistance' => $maxDistanceKm / 111));
Warning: $near changed in Mongo 2.4, now it return only the first 100 items too: $near Doc for Mongo 2.4
If you don't need to sort by distance, use $geoWithin
(or $within
in Mongo 2.2), then you can sort by other fields and paginate:
$query->field('coordinates')
->equals(array('$within' => array('$center' => array(array($lat, $lon)
, $maxDistanceKm / 111) ) ));