当mongodb的版本大于3.5,在使用aggregate查询时,需要有cursor参数。
Thinkphp5的mongodb驱动存在这个bug,修复步骤:
1,找到Builder.php,在aggregate方法里添加一行
$cmd = [
'aggregate' => $options['table'],
'allowDiskUse' => true,
'pipeline' => $pipeline,
'cursor' => (object)[]
];
2,找到Query.php,在aggregate方法里面修改
public function aggregate($aggregate, $field)
{
$result = $this->cmd('aggregate', [$aggregate, $field]);
// return isset($result[0]['result'][0]['aggregate']) ? $result[0]['result'][0]['aggregate'] : 0;
return isset($result[0]['aggregate']) ? $result[0]['aggregate'] : 0;
}
这样就可以正常使用mongodb驱动里的聚合方法了。
网友评论