|
['$group' => ['_id' => $group, 'aggregate' => ['$' . $fun => '$' . $field]]], |
应该单独判断“ ['$group' => ['_id' => $group, 'aggregate' => ['$' . $fun => '$' . $field]]],”中的$fun,如果时统计,可以直接设置为['$sum' => 1],否则当field字段为非数字时无法统计。
我的解决方法是:
if ($fun == 'count') {
$pipeline = [
['$match' => (object) $this->parseWhere($query, $options['where'])],
['$group' => ['_id' => $group, 'aggregate' => ['$sum' => 1]]],
];
} else {
$pipeline = [
['$match' => (object) $this->parseWhere($query, $options['where'])],
['$group' => ['_id' => $group, 'aggregate' => ['$' . $fun => '$' . $field]]],
];
}
think-mongo/src/Builder.php
Line 525 in 4e01004
应该单独判断“ ['$group' => ['_id' => $group, 'aggregate' => ['$' . $fun => '$' . $field]]],”中的$fun,如果时统计,可以直接设置为['$sum' => 1],否则当field字段为非数字时无法统计。
我的解决方法是:
if ($fun == 'count') {
$pipeline = [
['$match' => (object) $this->parseWhere($query, $options['where'])],
['$group' => ['_id' => $group, 'aggregate' => ['$sum' => 1]]],
];
} else {
$pipeline = [
['$match' => (object) $this->parseWhere($query, $options['where'])],
['$group' => ['_id' => $group, 'aggregate' => ['$' . $fun => '$' . $field]]],
];
}