4

I'm using a geoNear() to calculate distances between objects in my Mongo database. The query works perfectly, even with addition field filters such as ->field('name')->equals($name) etc...

This automatically populates a mapped field @ODM\Distance on the object.

$this->getQueryBuilder()
->geoNear((float) $query['near_longitude'], (float) $query['near_latitude'])
->spherical(true)
->distanceMultiplier(self::EARTH_RD_KM);

If I add an ->field('id')->in($array) however this distance is suddenly 0. I'm not really sure where the information is lost. Is this a limitation on how $in works on MongoDB?

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
Simon
  • 1,643
  • 7
  • 30
  • 61
  • What do you want to achieve? What's your final query which gives wrong result or correct result? Update your question with these details. – Somnath Muluk Dec 07 '15 at 12:42
  • where are you adding ->field('id')->in($array)? I'm guessing you are changing the meaning of your query - show the full query with this new part added. – Asya Kamsky Dec 08 '15 at 00:03
  • $this->getQueryBuilder()->field('id')->in($filters['ids_set']); $filters['ids_set'] is an array of ids and it gives me back the correct subset of results but without distance, which is now zero. If I don't filter with ->in it gives me the distance. @AsyaKamsky – Simon Dec 08 '15 at 14:09
  • Anyway, I think you can probably look at answer to this question here which shows an example of correct syntax: http://stackoverflow.com/a/14756264/431012 – Asya Kamsky Dec 08 '15 at 17:35

1 Answers1

1

You need to use "maxDistance" function

like this;

$this->getQueryBuilder()
    ->geoNear((float) $query['near_longitude'], (float)$query['near_latitude'])      

    ->spherical(true) 

    ->maxDistance(self::YOUR_DESIRED_KM_RADIUS / self::EARTH_RD_KM)

    ->distanceMultiplier(self::EARTH_RD_KM);

That way it sets a radius for your query.

ag0702
  • 381
  • 1
  • 6
  • 18