我目前正在使用当前的PHP MongoDB\Driver。
我需要使用geoNear
查询从当前位置获取点。所需的2dsphere index
已设置,查询在控制台中工作并提供多个结果:
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 MongoDBDriverCommand($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."
}
]"
我找不到适合我的情况的解决方案,也找不到此错误的更多信息。
如果我将Command
更改为Query
,执行不会失败,但没有结果。
mongodb版本是3.2版本,我的PHP版本是PHP版本7.0.16-4 + deb.sury.org~trusty+1,mongodb Exension版本是1.2.3
可以通过以下方式将聚合与新驱动程序一起使用。
$pipeline = array(array(
'$geoNear'=> array(
'near' => array(
52.264633,
6.12485
),
'spherical' => true,
'maxDistance' => 1000,
'distanceField' => "distance"
)));
$cmd = new MongoDBDriverCommand([
'aggregate' => 'pois',
'pipeline' => $pipeline
]);
$returnCursor = $this->conn->executeCommand("database", $cmd);
$arrReturn = $returnCursor->toArray();
Mongo还有一个库,它扩展了驱动程序的默认功能,使其更加用户友好
但由于它没有内置在PHP网站中,所以很容易错过
MongoDB\Collection::aggregate($pipeline, $options(
哪里
$pipeline = array(array(
'$geoNear'=> array(
'near' => array(
52.264633,
6.12485
),
'spherical' => true,
'maxDistance' => 1000,
'distanceField' => "distance"
)
));