Skip to content

Commit 6bb72e8

Browse files
committed
Merge pull request #29 from andrewryno/subdocument-aggregation
Modify the aggregation logic to handle keys for subdocuments.
2 parents bc686c9 + 85bad94 commit 6bb72e8

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/Jenssegers/Mongodb/Builder.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,16 @@ public function getFresh($columns = array('*'))
114114
// Pass other functions directly
115115
else
116116
{
117-
$group[$column] = array('$' . $function => '$' . $column);
117+
// Normally this aggregate function would overwrite the
118+
// $last group set above, but since we are modifying
119+
// the string, we need to unset it directly.
120+
if (isset($group[$column]))
121+
{
122+
unset($group[$column]);
123+
}
124+
125+
$key = str_replace('.', '_', $column);
126+
$group[$key] = array('$' . $function => '$' . $column);
118127
}
119128
}
120129
}
@@ -203,7 +212,8 @@ public function aggregate($function, $columns = array('*'))
203212

204213
if (isset($results[0]))
205214
{
206-
return $results[0][$columns[0]];
215+
$key = str_replace('.', '_', $columns[0]);
216+
return $results[0][$key];
207217
}
208218
}
209219

@@ -270,7 +280,7 @@ public function insert(array $values)
270280
{
271281
// As soon as we find a value that is not an array we assume the user is
272282
// inserting a single document.
273-
if (!is_array($value))
283+
if (!is_array($value))
274284
{
275285
$batch = false; break;
276286
}
@@ -522,7 +532,7 @@ protected function performUpdate($query)
522532

523533
/**
524534
* Convert a key to MongoID if needed
525-
*
535+
*
526536
* @param mixed $id
527537
* @return mixed
528538
*/
@@ -548,7 +558,7 @@ protected function compileWheres()
548558
// The new list of compiled wheres
549559
$wheres = array();
550560

551-
foreach ($this->wheres as $i => &$where)
561+
foreach ($this->wheres as $i => &$where)
552562
{
553563
// Convert id's
554564
if (isset($where['column']) && $where['column'] == '_id')

tests/QueryTest.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
55

66
public static function setUpBeforeClass()
77
{
8-
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
9-
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
10-
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
11-
User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user'));
12-
User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user'));
13-
User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user'));
14-
User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user'));
15-
User::create(array('name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin'));
8+
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin', 'subdocument' => array('age' => 35)));
9+
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin', 'subdocument' => array('age' => 33)));
10+
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user', 'subdocument' => array('age' => 13)));
11+
User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user', 'subdocument' => array('age' => 37)));
12+
User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user', 'subdocument' => array('age' => 23)));
13+
User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user', 'subdocument' => array('age' => 35)));
14+
User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user', 'subdocument' => array('age' => 33)));
15+
User::create(array('name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin', 'subdocument' => array('age' => 35)));
1616
User::create(array('name' => 'Error', 'age' => null, 'title' => null));
1717
}
1818

@@ -176,9 +176,14 @@ public function testAggregates()
176176
$this->assertEquals(30.5, User::avg('age'));
177177
$this->assertEquals(244, User::sum('age'));
178178

179+
$this->assertEquals(37, User::max('subdocument.age'));
180+
$this->assertEquals(13, User::min('subdocument.age'));
181+
$this->assertEquals(30.5, User::avg('subdocument.age'));
182+
$this->assertEquals(244, User::sum('subdocument.age'));
183+
179184
$this->assertEquals(35, User::where('title', 'admin')->max('age'));
180185
$this->assertEquals(37, User::where('title', 'user')->max('age'));
181-
186+
182187
$this->assertEquals(33, User::where('title', 'admin')->min('age'));
183188
$this->assertEquals(13, User::where('title', 'user')->min('age'));
184189
}

0 commit comments

Comments
 (0)