laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

aggregation - count of group

Open rezayavari66 opened this issue 6 years ago • 9 comments

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'];
    }
}`

rezayavari66 avatar May 22 '19 22:05 rezayavari66

image

when i comment this line. it's can be work. why this?

mouyong avatar Jul 19 '20 12:07 mouyong

image

this is my code.

mouyong avatar Jul 19 '20 12:07 mouyong

Same issue here.

kamilkozak avatar Aug 08 '20 11:08 kamilkozak

Did anyone find a work-around for this issue?

gilesa avatar Jun 18 '21 15:06 gilesa

Me too. Did anyone solved it yet? laravel5.7 jenssegers 3.4

------------ Fields success_num failed_num are int. ------ ----- Part of the code: return self::query() ->aggregate('sum', ['success_num', 'failed_num']) ->where(function ($query) use ($params) { !empty($params['start_date']) && $query->where("date", ">=", $params['start_date']); !empty($params['end_date']) && $query->where("date", "<=", $params['end_date']); })->groupBy('date') ->get('date', 'success_num', 'failed_num']) ->toArray ();

MrLining avatar Feb 18 '22 08:02 MrLining

------------ Final solution, FYI:------------

return self::raw(function($collection) use ($params) { $whereData = [ 'date' => ['$gte' => '0'], ]; !empty($params['start_date']) && empty($params['end_date']) && $whereData['date'] = ['$gte' => $params['start_date']]; empty($params['start_date']) && !empty($params['end_date']) && $whereData['date'] = ['$lte' => $params['end_date']]; !empty($params['start_date']) && !empty($params['end_date']) && $whereData['date'] = ['$gte' => $params['start_date'], '$lte' => $params['end_date']];

        return $collection->aggregate([
            ['$match' => $whereData],
            ['$group' => [
                '_id' => '$date',
               'success_num' => ['$sum' => '$success_num'],
                'failed_num' => ['$sum' => '$failed_num'],
            ]]
        ]);
    });

MrLining avatar Feb 18 '22 11:02 MrLining

image

this is my code.

Same to me...

maoleng avatar Jun 15 '22 07:06 maoleng

The issue with aggregate seems to be occurs also in v3.9.2. After commenting out $this->aggregate = null; in src/Query/Builder.php ~469 it works as expected.

$q = Model::query();
$q->aggregate('count', ['field']);
$result = $q->first();

Actual result: First model

Result after commenting out $this->aggregate = null;

// $result:
Array
(
    [_id] => 
    [aggregate] => 500
)

@jenssegers how can I use aggregation methods without using raw expressions? Am I missing something or do I misunderstand the aggregate method?

linushstge avatar Dec 19 '22 11:12 linushstge

The issue with aggregate seems to be occurs also in v3.9.2. After commenting out $this->aggregate = null; in src/Query/Builder.php ~469 it works as expected.

$q = Model::query();
$q->aggregate('count', ['field']);
$result = $q->first();

Actual result: First model

Result after commenting out $this->aggregate = null;

// $result:
Array
(
    [_id] => 
    [aggregate] => 500
)

@jenssegers how can I use aggregation methods without using raw expressions? Am I missing something or do I misunderstand the aggregate method?

any solution?

nemesisKO avatar Mar 31 '23 22:03 nemesisKO