Skip to content

Aggregate subdocument arrays #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public function getFresh($columns = [])
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
if ($this->groups or $this->aggregate or $this->paginating) {
$group = [];
$unwinds = [];

// Add grouping columns to the $group part of the aggregation pipeline.
if ($this->groups) {
Expand All @@ -212,6 +213,13 @@ public function getFresh($columns = [])
$function = $this->aggregate['function'];

foreach ($this->aggregate['columns'] as $column) {
// Add unwind if a subdocument array should be aggregated
// column: subarray.price => {$unwind: '$subarray'}
if (count($splitColumns = explode('.*.', $column)) == 2) {
$unwinds[] = $splitColumns[0];
$column = implode('.', $splitColumns);
}

// Translate count into sum.
if ($function == 'count') {
$group['aggregate'] = ['$sum' => 1];
Expand Down Expand Up @@ -241,6 +249,12 @@ public function getFresh($columns = [])
if ($wheres) {
$pipeline[] = ['$match' => $wheres];
}

// apply unwinds for subdocument array aggregation
foreach ($unwinds as $unwind) {
$pipeline[] = ['$unwind' => '$' . $unwind];
}

if ($group) {
$pipeline[] = ['$group' => $group];
}
Expand Down
16 changes: 16 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,22 @@ public function testSubdocumentAggregate()
$this->assertEquals(16.25, DB::collection('items')->avg('amount.hidden'));
}

public function testSubdocumentArrayAggregate()
{
DB::collection('items')->insert([
['name' => 'knife', 'amount' => [['hidden' => 10, 'found' => 3],['hidden' => 5, 'found' => 2]]],
['name' => 'fork', 'amount' => [['hidden' => 35, 'found' => 12],['hidden' => 7, 'found' => 17],['hidden' => 1, 'found' => 19]]],
['name' => 'spoon', 'amount' => [['hidden' => 14, 'found' => 21]]],
['name' => 'teaspoon', 'amount' => []],
]);

$this->assertEquals(72, DB::collection('items')->sum('amount.*.hidden'));
$this->assertEquals(6, DB::collection('items')->count('amount.*.hidden'));
$this->assertEquals(1, DB::collection('items')->min('amount.*.hidden'));
$this->assertEquals(35, DB::collection('items')->max('amount.*.hidden'));
$this->assertEquals(12, DB::collection('items')->avg('amount.*.hidden'));
}

public function testUpsert()
{
DB::collection('items')->where('name', 'knife')
Expand Down