laravel-mongodb
laravel-mongodb copied to clipboard
aggregation - count of group
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'];
}
}`

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

this is my code.
Same issue here.
Did anyone find a work-around for this issue?
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 ();
------------ 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'],
]]
]);
});
this is my code.
Same to me...
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?
The issue with
aggregateseems to be occurs also in v3.9.2. After commenting out$this->aggregate = null;insrc/Query/Builder.php ~469it 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?