Skip to content

Commit efbf965

Browse files
committed
Adding mongodb specific operators, fixes mongodb#82 and mongodb#81
1 parent 3aad766 commit efbf965

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

Diff for: README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ This will allow you to use your registered alias like:
9494
Query Builder
9595
-------------
9696

97-
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`.
97+
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.
9898

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

239+
### MongoDB specific operators
240+
241+
**Exists**
242+
243+
Matches documents that have the specified field.
244+
245+
User::where('age', 'exists', true)->get();
246+
247+
**All**
248+
249+
Matches arrays that contain all elements specified in the query.
250+
251+
User::where('roles', 'all', array('moderator', 'author'))->get();
252+
253+
**Size**
254+
255+
Selects documents if the array field is a specified size.
256+
257+
User::where('tags', 'size', 3)->get();
258+
259+
**Type**
260+
261+
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
262+
263+
User::where('age', 'type', 2)->get();
264+
265+
**Mod**
266+
267+
Performs a modulo operation on the value of a field and selects documents with a specified result.
268+
269+
User::where('age', 'mod', array(10, 0))->get();
270+
271+
**Where**
272+
273+
Matches documents that satisfy a JavaScript expression. For more information check http://docs.mongodb.org/manual/reference/operator/query/where/#op._S_where
274+
239275
### Inserts, updates and deletes
240276

241277
All basic insert, update, delete and select methods should be implemented.

Diff for: src/Jenssegers/Mongodb/Query/Builder.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ class Builder extends \Illuminate\Database\Query\Builder {
1818
protected $collection;
1919

2020
/**
21-
* All of the available operators.
21+
* All of the available clause operators.
22+
*
23+
* @var array
24+
*/
25+
protected $operators = array(
26+
'=', '<', '>', '<=', '>=', '<>', '!=',
27+
'like', 'not like', 'between', 'ilike',
28+
'&', '|', '^', '<<', '>>',
29+
'exists', 'type', 'mod', 'where', 'all', 'size',
30+
);
31+
32+
/**
33+
* Operator conversion.
2234
*
2335
* @var array
2436
*/
@@ -701,10 +713,14 @@ protected function compileWhereBasic($where)
701713
{
702714
$query = array($column => $value);
703715
}
704-
else
716+
else if (array_key_exists($operator, $this->conversion))
705717
{
706718
$query = array($column => array($this->conversion[$operator] => $value));
707719
}
720+
else
721+
{
722+
$query = array($column => array('$' . $operator => $value));
723+
}
708724

709725
return $query;
710726
}

Diff for: tests/QueryBuilderTest.php

+53
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,57 @@ public function testDates()
445445
$this->assertEquals(2, count($users));
446446
}
447447

448+
public function testOperators()
449+
{
450+
DB::collection('users')->insert(array(
451+
array('name' => 'John Doe', 'age' => 30),
452+
array('name' => 'Jane Doe'),
453+
array('name' => 'Robert Roe', 'age' => 'thirty-one'),
454+
));
455+
456+
$results = DB::collection('users')->where('age', 'exists', true)->get();
457+
$this->assertEquals(2, count($results));
458+
$this->assertEquals('John Doe', $results[0]['name']);
459+
460+
$results = DB::collection('users')->where('age', 'exists', false)->get();
461+
$this->assertEquals(1, count($results));
462+
$this->assertEquals('Jane Doe', $results[0]['name']);
463+
464+
$results = DB::collection('users')->where('age', 'type', 2)->get();
465+
$this->assertEquals(1, count($results));
466+
$this->assertEquals('Robert Roe', $results[0]['name']);
467+
468+
$results = DB::collection('users')->where('age', 'mod', array(15, 0))->get();
469+
$this->assertEquals(1, count($results));
470+
$this->assertEquals('John Doe', $results[0]['name']);
471+
472+
$results = DB::collection('users')->where('age', 'mod', array(29, 1))->get();
473+
$this->assertEquals(1, count($results));
474+
$this->assertEquals('John Doe', $results[0]['name']);
475+
476+
$results = DB::collection('users')->where('age', 'mod', array(14, 0))->get();
477+
$this->assertEquals(0, count($results));
478+
479+
DB::collection('items')->insert(array(
480+
array('name' => 'fork', 'tags' => array('sharp', 'pointy')),
481+
array('name' => 'spork', 'tags' => array('sharp', 'pointy', 'round', 'bowl')),
482+
array('name' => 'spoon', 'tags' => array('round', 'bowl')),
483+
));
484+
485+
$results = DB::collection('items')->where('tags', 'all', array('sharp', 'pointy'))->get();
486+
$this->assertEquals(2, count($results));
487+
488+
$results = DB::collection('items')->where('tags', 'all', array('sharp', 'round'))->get();
489+
$this->assertEquals(1, count($results));
490+
491+
$results = DB::collection('items')->where('tags', 'size', 2)->get();
492+
$this->assertEquals(2, count($results));
493+
494+
$results = DB::collection('items')->where('tags', 'size', 3)->get();
495+
$this->assertEquals(0, count($results));
496+
497+
$results = DB::collection('items')->where('tags', 'size', 4)->get();
498+
$this->assertEquals(1, count($results));
499+
}
500+
448501
}

0 commit comments

Comments
 (0)