Skip to content

聚合统计非数字类型字段问题 #82

@korn519

Description

@korn519

['$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]]],
];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions