Skip to content

aggregation - count of group #1763

Open
@rezayavari66

Description

@rezayavari66

I think find out an little mistake in aggregation:
My mongo query is
db.project_docs.aggregate([ {"$group" : {_id:"$tag", count:{$sum:1}}} ])
above query in package:
ProjectDoc->groupBy('tag')->aggregate('count',['tag'])->get();
but the result is different.
In package after the run aggregate the selected model returned and after calling get method the aggregate doesn't effect the result.
I trace the code and got to the Jenssegers\Mongodb\Query\Builder file.
In 'aggregate' after calling get method null assigned to $this->aggregate variable, which is why the result is incorrect
Jenssegers\Mongodb\Query\Builder
`

public function aggregate($function, $columns = [])
{
    $this->aggregate = compact('function', 'columns');

    $previousColumns = $this->columns;
    // We will also back up the select bindings since the select clause will be
    // removed when performing the aggregate function. Once the query is run
    // we will add the bindings back onto this query so they can get used.
    $previousSelectBindings = $this->bindings['select'];

    $this->bindings['select'] = [];

    $results = $this->get($columns);
    // Once we have executed the query, we will reset the aggregate property so
    // that more select queries can be executed against the database without
    // the aggregate value getting in the way when the grammar builds it.
    $this->aggregate = null;
    $this->columns = $previousColumns;
    $this->bindings['select'] = $previousSelectBindings;

    if (isset($results[0])) {

        $result = (array) $results[0];
        return $result['aggregate'];
    }
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs investigationNeed investigation about bugs described in issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions