Commit 5bd7ee64 authored by Jens Segers's avatar Jens Segers

Adding query projections, fixes #267

parent 79b6230a
......@@ -564,6 +564,12 @@ Update or insert a document. Additional options for the update method are passed
DB::collection('users')->where('name', 'John')
->update($data, array('upsert' => true));
**Projections**
You can apply projections to your queries using the `project()` method.
DB::collection('items')->project(array('tags' => array('$slice' => 1)))->get();
**Push**
Add an items to an array.
......
......@@ -18,6 +18,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
protected $collection;
/**
* The column projections.
*
* @var array
*/
public $projections;
/**
* All of the available clause operators.
*
......@@ -59,6 +66,19 @@ class Builder extends \Illuminate\Database\Query\Builder {
$this->connection = $connection;
}
/**
* Set the projections.
*
* @param array $columns
* @return $this
*/
public function project($columns)
{
$this->projections = is_array($columns) ? $columns : func_get_args();
return $this;
}
/**
* Execute a query for a single record by ID.
*
......@@ -155,6 +175,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
if ($this->orders) $pipeline[] = array('$sort' => $this->orders);
if ($this->offset) $pipeline[] = array('$skip' => $this->offset);
if ($this->limit) $pipeline[] = array('$limit' => $this->limit);
if ($this->projections) $pipeline[] = array('$project' => $this->projections);
// Execute aggregation
$results = $this->collection->aggregate($pipeline);
......@@ -179,11 +200,19 @@ class Builder extends \Illuminate\Database\Query\Builder {
else
{
$columns = array();
// Convert select columns to simple projections.
foreach ($this->columns as $column)
{
$columns[$column] = true;
}
// Add custom projections.
if ($this->projections)
{
$columns = array_merge($columns, $this->projections);
}
// Execute query and get MongoCursor
$cursor = $this->collection->find($wheres, $columns);
......@@ -543,8 +572,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
public function pull($column, $value = null)
{
var_dump($value);
// Check if we passed an associative array.
$multipleValues = (is_array($value) and array_keys($value) === range(0, count($value) - 1));
......
......@@ -611,4 +611,20 @@ class QueryBuilderTest extends TestCase {
$this->assertEquals(1, $user['age']);
}
public function testProjections()
{
DB::collection('items')->insert(array(
array('name' => 'fork', 'tags' => array('sharp', 'pointy')),
array('name' => 'spork', 'tags' => array('sharp', 'pointy', 'round', 'bowl')),
array('name' => 'spoon', 'tags' => array('round', 'bowl')),
));
$results = DB::collection('items')->project(array('tags' => array('$slice' => 1)))->get();
foreach ($results as $result)
{
$this->assertEquals(1, count($result['tags']));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment