1

I am trying to see how to do geoNear command without rewriting doctrine-mongodb project. I have looked through Expr.php and Builder.php files and I found no references to geoNear command, how can I get this functionality in my Doctrine MongoDB ODM ?

thanks

adlawson
  • 6,303
  • 1
  • 35
  • 46
MongoUser
  • 11
  • 2

2 Answers2

1

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) ) ));
Madarco
  • 2,084
  • 18
  • 26
  • So you cannot paginate (limit) with `$near`? – Aamir Afridi Jan 04 '16 at 00:54
  • 1
    Last time I've checked: no. With newer Mongo use the aggregate framework, however consider that many operations are done in memory, and so limited to 16Mb of results – Madarco Jan 06 '16 at 14:33
0

How old is your library version ?

There is an implementation of geoNear command available on Doctrine MongoDb mappers's trunk, $qb->near(array($x, $y)). But have tested it just very quickly, i can't tell you how good it works in an extensive usage.

Frederik Eychenié
  • 1,253
  • 7
  • 8