0

I'm having issue to get mongodb distance value in my document when I do geoNear queries.

I have 3 documents involved : Venue, that embed Location, that embed Coordinates.

Here is a "light" version of my model

Venue :

/**
 * Venue
 * 
 * @ODM\Document(repositoryClass="CLabs\VenueBundle\Document\VenueRepository")
 */
class Venue
{
    /**
     * @var integer
     *
     * @ODM\Id(strategy="auto")
     */
    protected $id;

    /**
     * @var float
     *
     * @ODM\Distance
     * @ODM\NotSaved
     */
    public $distance;

    /**
     * @var \Location
     *
     * @ODM\EmbedOne(
     *      targetDocument="CLabs\LocationBundle\Document\Location"
     * )
     */
    protected $location;

    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set distance
     *
     * @param float $distance
     * @return self
     */
    public function setDistance($distance)
    {
        $this->distance = $distance;
        return $this;
    }

    /**
     * Get distance
     *
     * @return float $distance
     */
    public function getDistance()
    {
        return $this->distance;
    }

    /**
     * Set location
     *
     * @param CLabs\LocationBundle\Document\Location $location
     * @return self
     */
    public function setLocation(\CLabs\LocationBundle\Document\Location $location)
    {
        $this->location = $location;
        return $this;
    }

    /**
     * Get location
     *
     * @return CLabs\LocationBundle\Document\Location $location
     */
    public function getLocation()
    {
        return $this->location;
    }
}

Location :

/**
 * Location
 * 
 * @ODM\EmbeddedDocument
 * @ODM\Index(keys={"coordinates"="2d"})
 */
class Location
{
    /**
     * @var \Coordinates
     *
     * @ODM\EmbedOne(
     *      targetDocument="CLabs\LocationBundle\Document\Coordinates"
     * )
     */
    protected $coordinates;

    /**
     * Set coordinates
     *
     * @param CLabs\LocationBundle\Document\Coordinates $coordinates
     * @return self
     */
    public function setCoordinates(\CLabs\LocationBundle\Document\Coordinates $coordinates)
    {
        $this->coordinates = $coordinates;
        return $this;
    }

    /**
     * Get coordinates
     *
     * @return CLabs\LocationBundle\Document\Coordinates $coordinates
     */
    public function getCoordinates()
    {
        return $this->coordinates;
    }
}

Coordinates :

/**
 * Coordinates
 * @ODM\EmbeddedDocument
 */
class Coordinates
{
    /**
     * @var string
     *
     * @ODM\Float
     */
    protected $lng;

    /**
     * @var string
     *
     * @ODM\Float
     */
    protected $lat;

    /**
     * Set lng
     *
     * @param float $lng
     * @return self
     */
    public function setLng($lng)
    {
        $this->lng = $lng;
        return $this;
    }

    /**
     * Get lng
     *
     * @return float $lng
     */
    public function getLng()
    {
        return $this->lng;
    }

    /**
     * Set lat
     *
     * @param float $lat
     * @return self
     */
    public function setLat($lat)
    {
        $this->lat = $lat;
        return $this;
    }

    /**
     * Get lat
     *
     * @return float $lat
     */
    public function getLat()
    {
        return $this->lat;
    }
}

From this model, when I do a geoNear query on VenueRepository like this one :

$kmsMultiplier = 6378.137;

return $this->createQueryBuilder()
    ->geoNear((float)$lng, (float)$lat)
    ->maxDistance($maxDistance/$kmsMultiplier)
    ->spherical(true)
    // Convert radians to kilometers
    ->distanceMultiplier($kmsMultiplier)
    ->getQuery()
    ->execute();

I extract my object collection by doing '$result->toArray()' (or simply a foreach on results), then I would like to get distance from my object like it should be possible as explained in doctrine documentation.

But when I do $venue->getDistance() it always returns null, even if mongodb has properly calculated the distances.

Here is a sample of the result of this query :

    "elements": [
      {
        "id": "559f7b728b8ff7c1127b23c8",
        "location": {
          "coordinates": {
            "lng": 5.4190150243677,
            "lat": 43.533426029669
          }
        }
      }...
    ],
    "commandResult": {
      "results": [
        {
          "dis": 3.1465013027305,
          "obj": {
            "_id": {
              "$id": "559f7b728b8ff7c1127b23c8"
            },
            "location": {
              "coordinates": {
                "lng": 5.4190150243677,
                "lat": 43.533426029669
              }
            }
          }
        }...
      ],
      "stats": {
        "nscanned": 3,
        "objectsLoaded": 3,
        "avgDistance": 3.8863491981201,
        "maxDistance": 4.3498686266835,
        "time": 0
      },
      "ok": 1
    }

I tried to remove my get/setDistance from Venue document, move $distance declaration in Location document but nothing works, I can't get distance from $object->getDistance() nor $object->distance.

I could handle this property mapping myself, but it would be much more handy to use Doctrine method instead, well, if it works... I can't figure out where my mistake is, or if my code is to blame.

Here is a sample of my composer.json:

"symfony/symfony": "v2.6.4",
"doctrine/mongodb": "v1.1.8",
"doctrine/mongodb-odm": "v1.0.0-BETA13",
"doctrine/mongodb-odm-bundle": "v3.0.0"

I have mongodb 2.6.11 installed on a debian VM (Linux wheezy64 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u3 x86_64 GNU/Linux).

Do you think it's a Doctrine ODM issue ? Am I the only one that having this issue ?

Any help appreciated

Julz
  • 46
  • 3

1 Answers1

0

After some digging, I found the solution by myself.

First I updated some bundles :

  • doctrine/mongodb => 1.2.0
  • doctrine/mongodb-odm => 1.0.1

But the issue still occurred.

In fact, it seems like there was a conflict in my model between the annotations @NotSaved and @Distance. I've previously added @NotSaved annotation because I used to have mapping errors in former bundles versions when doing geoNear queries.

Those issues must have been resolved in latest bundle versions because after removing @NotSaved annotation from distance field, I now get the distance value in geoNear queries, also Doctrine doesn't try anymore to save distance field in db.

Julz
  • 46
  • 3