Skip to content

Commit

Permalink
Adding mongodb specific operators, fixes #82 and #81
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Dec 8, 2013
1 parent ede3e23 commit 288c3d7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ This will allow you to use your registered alias like:
Query Builder
-------------

The database driver plugs right into the original query builder. When using mongodb connections you will be able to build fluent queries to perform database operations. For your convenience, there is a `collection` alias for `table` as well as some additional mongodb specific operations like `push` and `pull`.
The database driver plugs right into the original query builder. When using mongodb connections you will be able to build fluent queries to perform database operations. For your convenience, there is a `collection` alias for `table` as well as some additional mongodb specific operators/operations.

// With custom connection
$user = DB::connection('mongodb')->collection('users')->get();
Expand Down Expand Up @@ -236,6 +236,42 @@ You may also specify additional columns to update:
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));

### MongoDB specific operators

**Exists**

Matches documents that have the specified field.

User::where('age', 'exists', true)->get();

**All**

Matches arrays that contain all elements specified in the query.

User::where('roles', 'all', array('moderator', 'author'))->get();

**Size**

Selects documents if the array field is a specified size.

User::where('tags', 'size', 3)->get();

**Type**

Selects documents if a field is of the specified type. For more information check: http://docs.mongodb.org/manual/reference/operator/query/type/#op._S_type

User::where('age', 'type', 2)->get();

**Mod**

Performs a modulo operation on the value of a field and selects documents with a specified result.

User::where('age', 'mod', array(10, 0))->get();

**Where**

Matches documents that satisfy a JavaScript expression. For more information check http://docs.mongodb.org/manual/reference/operator/query/where/#op._S_where

### Inserts, updates and deletes

All basic insert, update, delete and select methods should be implemented.
Expand Down
20 changes: 18 additions & 2 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ class Builder extends \Illuminate\Database\Query\Builder {
protected $collection;

/**
* All of the available operators.
* All of the available clause operators.
*
* @var array
*/
protected $operators = array(
'=', '<', '>', '<=', '>=', '<>', '!=',
'like', 'not like', 'between', 'ilike',
'&', '|', '^', '<<', '>>',
'exists', 'type', 'mod', 'where', 'all', 'size',
);

/**
* Operator conversion.
*
* @var array
*/
Expand Down Expand Up @@ -701,10 +713,14 @@ protected function compileWhereBasic($where)
{
$query = array($column => $value);
}
else
else if (array_key_exists($operator, $this->conversion))
{
$query = array($column => array($this->conversion[$operator] => $value));
}
else
{
$query = array($column => array('$' . $operator => $value));
}

return $query;
}
Expand Down
53 changes: 53 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,57 @@ public function testDates()
$this->assertEquals(2, count($users));
}

public function testOperators()
{
DB::collection('users')->insert(array(
array('name' => 'John Doe', 'age' => 30),
array('name' => 'Jane Doe'),
array('name' => 'Robert Roe', 'age' => 'thirty-one'),
));

$results = DB::collection('users')->where('age', 'exists', true)->get();
$this->assertEquals(2, count($results));
$this->assertEquals('John Doe', $results[0]['name']);

$results = DB::collection('users')->where('age', 'exists', false)->get();
$this->assertEquals(1, count($results));
$this->assertEquals('Jane Doe', $results[0]['name']);

$results = DB::collection('users')->where('age', 'type', 2)->get();
$this->assertEquals(1, count($results));
$this->assertEquals('Robert Roe', $results[0]['name']);

$results = DB::collection('users')->where('age', 'mod', array(15, 0))->get();
$this->assertEquals(1, count($results));
$this->assertEquals('John Doe', $results[0]['name']);

$results = DB::collection('users')->where('age', 'mod', array(29, 1))->get();
$this->assertEquals(1, count($results));
$this->assertEquals('John Doe', $results[0]['name']);

$results = DB::collection('users')->where('age', 'mod', array(14, 0))->get();
$this->assertEquals(0, count($results));

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')->where('tags', 'all', array('sharp', 'pointy'))->get();
$this->assertEquals(2, count($results));

$results = DB::collection('items')->where('tags', 'all', array('sharp', 'round'))->get();
$this->assertEquals(1, count($results));

$results = DB::collection('items')->where('tags', 'size', 2)->get();
$this->assertEquals(2, count($results));

$results = DB::collection('items')->where('tags', 'size', 3)->get();
$this->assertEquals(0, count($results));

$results = DB::collection('items')->where('tags', 'size', 4)->get();
$this->assertEquals(1, count($results));
}

}

0 comments on commit 288c3d7

Please sign in to comment.