当我进行 geoNear 查询时,我在文档中获取 mongodb 距离值时遇到问题。
我涉及 3 个文件:地点,嵌入位置,嵌入坐标。
这是我的模型的“轻”版本
地点:
/**
* 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
*
* @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
* @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;
}
}
从这个模型中,当我像这样在 VenueRepository 上进行 geoNear 查询时:
$kmsMultiplier = 6378.137;
return $this->createQueryBuilder()
->geoNear((float)$lng, (float)$lat)
->maxDistance($maxDistance/$kmsMultiplier)
->spherical(true)
// Convert radians to kilometers
->distanceMultiplier($kmsMultiplier)
->getQuery()
->execute();
我通过执行 '$result->toArray()' (或简单地对结果进行 foreach)提取我的对象集合,然后我想与我的对象保持距离,就像在教义文档中解释的那样。
但是当我这样做时,$venue->getDistance()
它总是返回null
,即使 mongodb 已经正确计算了距离。
以下是此查询结果的示例:
"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
}
我试图从 Venue 文档中删除我的 get/setDistance,在 Location 文档中移动 $distance 声明,但没有任何效果,我无法从$object->getDistance()
nor获得距离$object->distance
。
我可以自己处理这个属性映射,但是使用 Doctrine 方法会更方便,好吧,如果它有效......我不知道我的错误在哪里,或者我的代码是否应该受到指责。
这是我的 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"
我在 debian VM 上安装了 mongodb 2.6.11(Linux wheezy64 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u3 x86_64 GNU/Linux)。
您认为这是 Doctrine ODM 问题吗?我是唯一有这个问题的人吗?
任何帮助表示赞赏