Commit 288c3d70 authored by Jens Segers's avatar Jens Segers

Adding mongodb specific operators, fixes #82 and #81

parent ede3e230
......@@ -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();
......@@ -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.
......
......@@ -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
*/
......@@ -701,10 +713,14 @@ class Builder extends \Illuminate\Database\Query\Builder {
{
$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;
}
......
......@@ -445,4 +445,57 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$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));
}
}
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