Skip to content

Commit 5c438b1

Browse files
committed
Execute raw expressions, fixes #19
1 parent 5479935 commit 5c438b1

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ These expressions will be injected directly into the query.
230230

231231
User::whereRaw(array('age' => array('$gt' => 30, '$lt' => 40)))->get();
232232

233+
You can also perform raw expressions on the internal MongoCollection object, note that this will return the original response, and not a collection of models.
234+
235+
User::raw(function($collection)
236+
{
237+
return $collection->find();
238+
});
239+
233240
**Query Caching**
234241

235242
You may easily cache the results of a query using the remember method:

src/Jenssegers/Mongodb/Builder.php

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use MongoID;
44
use MongoRegex;
5+
use Closure;
56

67
class Builder extends \Illuminate\Database\Query\Builder {
78

@@ -403,6 +404,20 @@ public function truncate()
403404
return (1 == (int) $result['ok']);
404405
}
405406

407+
/**
408+
* Create a raw database expression.
409+
*
410+
* @param closure $expression
411+
* @return mixed
412+
*/
413+
public function raw($expression)
414+
{
415+
if ($expression instanceof Closure)
416+
{
417+
return call_user_func($expression, $this->collection);
418+
}
419+
}
420+
406421
/**
407422
* Get a new instance of the query builder.
408423
*

tests/QueryTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,16 @@ public function testInArray()
8686
$this->assertEquals(1, count($items));
8787
}
8888

89+
public function testRaw()
90+
{
91+
DB::collection('users')->insert(array('name' => 'John Doe'));
92+
93+
$cursor = DB::collection('users')->raw(function($collection) {
94+
return $collection->find();
95+
});
96+
97+
$this->assertInstanceOf('MongoCursor', $cursor);
98+
$this->assertEquals(1, $cursor->count());
99+
}
100+
89101
}

0 commit comments

Comments
 (0)