如何将geoNear与当前的PHP MongoDB驱动程序一起使用?
我目前正在处理当前的问题 我需要使用如何将geoNear与当前的PHP MongoDB驱动程序一起使用?,php,mongodb,php-7,Php,Mongodb,Php 7,我目前正在处理当前的问题 我需要使用geoNear查询从我的当前位置获取点。所需的2dsphere索引已设置,查询在控制台中工作并提供多个结果: db.runCommand({geoNear: 'pois', near: [ 52.264633, 6.12485 ], spherical: true, maxDistance: 1000, distanceField: 'distance'}) 由于前面的方法已被弃用,因此我无法使用旧的aggregate函数 我现在正试图找到正确的方法来使用当
geoNear
查询从我的当前位置获取点。所需的2dsphere索引
已设置,查询在控制台中工作并提供多个结果:
db.runCommand({geoNear: 'pois', near: [ 52.264633, 6.12485 ], spherical: true, maxDistance: 1000, distanceField: 'distance'})
由于前面的方法已被弃用,因此我无法使用旧的aggregate
函数
我现在正试图找到正确的方法来使用当前的query
或Command
类构建所需的查询
我尝试了以下几点:
$query = array(
'geoNear' => 'pois',
"near" => array(
52.264633,
6.12485
),
"spherical" => true,
"maxDistance" => 1000,
"distanceField" => "distance"
);
$cmd = new MongoDB\Driver\Command($query);
$returnCursor = $this->conn->executeCommand("database.pois", $cmd);
$arrReturn = $returnCursor->toArray();
return $arrReturn;
如果使用此选项,我将返回以下运行时错误:
"exception": [
{
"type": "MongoDB\\Driver\\Exception\\RuntimeException",
"code": 18,
"message": "Failed to decode document from the server."
}
]"
我无法为我的案例找到解决方案,也无法找到有关此错误的更多信息
如果我将命令
更改为查询
,执行不会失败,但没有结果
我的mongodb版本是3.2,我的PHP版本是PHPversion7.0.16-4+deb.sury.org~trusty+1,mongodb扩展版本是version1.2.3您可以通过以下方式在新驱动程序中使用聚合
$pipeline = array(array(
'$geoNear'=> array(
'near' => array(
52.264633,
6.12485
),
'spherical' => true,
'maxDistance' => 1000,
'distanceField' => "distance"
)));
$cmd = new \MongoDB\Driver\Command([
'aggregate' => 'pois',
'pipeline' => $pipeline
]);
$returnCursor = $this->conn->executeCommand("database", $cmd);
$arrReturn = $returnCursor->toArray();
还有一个扩展了驱动程序的默认功能,使其更加用户友好
但由于它没有内置在php网站中,因此很容易被忽略
在哪里
$pipeline = array(array(
'$geoNear'=> array(
'near' => array(
52.264633,
6.12485
),
'spherical' => true,
'maxDistance' => 1000,
'distanceField' => "distance"
)
));
你在哪里找到这个?我找不到关于这个的东西。但是它很有效,谢谢。我完全忽略了这一点,谢谢你给我指出了正确的方向。