Skip to content

Commit 5bd7ee6

Browse files
committed
Adding query projections, fixes #267
1 parent 79b6230 commit 5bd7ee6

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,12 @@ Update or insert a document. Additional options for the update method are passed
564564
DB::collection('users')->where('name', 'John')
565565
->update($data, array('upsert' => true));
566566

567+
**Projections**
568+
569+
You can apply projections to your queries using the `project()` method.
570+
571+
DB::collection('items')->project(array('tags' => array('$slice' => 1)))->get();
572+
567573
**Push**
568574

569575
Add an items to an array.

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
1818
*/
1919
protected $collection;
2020

21+
/**
22+
* The column projections.
23+
*
24+
* @var array
25+
*/
26+
public $projections;
27+
2128
/**
2229
* All of the available clause operators.
2330
*
@@ -59,6 +66,19 @@ public function __construct(Connection $connection)
5966
$this->connection = $connection;
6067
}
6168

69+
/**
70+
* Set the projections.
71+
*
72+
* @param array $columns
73+
* @return $this
74+
*/
75+
public function project($columns)
76+
{
77+
$this->projections = is_array($columns) ? $columns : func_get_args();
78+
79+
return $this;
80+
}
81+
6282
/**
6383
* Execute a query for a single record by ID.
6484
*
@@ -152,9 +172,10 @@ public function getFresh($columns = array())
152172
$pipeline[] = array('$group' => $group);
153173

154174
// Apply order and limit
155-
if ($this->orders) $pipeline[] = array('$sort' => $this->orders);
156-
if ($this->offset) $pipeline[] = array('$skip' => $this->offset);
157-
if ($this->limit) $pipeline[] = array('$limit' => $this->limit);
175+
if ($this->orders) $pipeline[] = array('$sort' => $this->orders);
176+
if ($this->offset) $pipeline[] = array('$skip' => $this->offset);
177+
if ($this->limit) $pipeline[] = array('$limit' => $this->limit);
178+
if ($this->projections) $pipeline[] = array('$project' => $this->projections);
158179

159180
// Execute aggregation
160181
$results = $this->collection->aggregate($pipeline);
@@ -179,11 +200,19 @@ public function getFresh($columns = array())
179200
else
180201
{
181202
$columns = array();
203+
204+
// Convert select columns to simple projections.
182205
foreach ($this->columns as $column)
183206
{
184207
$columns[$column] = true;
185208
}
186209

210+
// Add custom projections.
211+
if ($this->projections)
212+
{
213+
$columns = array_merge($columns, $this->projections);
214+
}
215+
187216
// Execute query and get MongoCursor
188217
$cursor = $this->collection->find($wheres, $columns);
189218

@@ -543,8 +572,6 @@ public function push($column, $value = null, $unique = false)
543572
*/
544573
public function pull($column, $value = null)
545574
{
546-
var_dump($value);
547-
548575
// Check if we passed an associative array.
549576
$multipleValues = (is_array($value) and array_keys($value) === range(0, count($value) - 1));
550577

tests/QueryBuilderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,4 +611,20 @@ public function testIncrement()
611611
$this->assertEquals(1, $user['age']);
612612
}
613613

614+
public function testProjections()
615+
{
616+
DB::collection('items')->insert(array(
617+
array('name' => 'fork', 'tags' => array('sharp', 'pointy')),
618+
array('name' => 'spork', 'tags' => array('sharp', 'pointy', 'round', 'bowl')),
619+
array('name' => 'spoon', 'tags' => array('round', 'bowl')),
620+
));
621+
622+
$results = DB::collection('items')->project(array('tags' => array('$slice' => 1)))->get();
623+
624+
foreach ($results as $result)
625+
{
626+
$this->assertEquals(1, count($result['tags']));
627+
}
628+
}
629+
614630
}

0 commit comments

Comments
 (0)