The doctrine of MongoDB geoNear () dinstance is set to 0 when adding an additional request

I use geoNear()to calculate distances between objects in my Mongo database. The query works fine, even with input field filters such as ->field('name')->equals($name)etc.

This automatically fills the displayed @ODM \ Distance field on the object.

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

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

+4
source share
1 answer

You need to use the "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);

, .

0

Source: https://habr.com/ru/post/1618628/


All Articles