|
1 | 1 | Laravel MongoDB |
2 | 2 | =============== |
3 | 3 |
|
4 | | -[](https://packagist.org/packages/jenssegers/mongodb) [](https://packagist.org/packages/jenssegers/mongodb) [](https://travis-ci.org/jenssegers/Laravel-MongoDB) |
| 4 | +[](https://packagist.org/packages/jenssegers/mongodb) [](https://packagist.org/packages/jenssegers/mongodb) [](https://travis-ci.org/jenssegers/Laravel-MongoDB) [](https://coveralls.io/r/jenssegers/Laravel-MongoDB?branch=master) |
5 | 5 |
|
6 | 6 | An Eloquent model and Query builder with support for MongoDB, inspired by LMongo, but using the original Laravel methods. *This library extends the original Laravel classes, so it uses exactly the same methods.* |
7 | 7 |
|
@@ -44,7 +44,7 @@ You can connect to multiple servers or replica sets with the following configura |
44 | 44 |
|
45 | 45 | 'mongodb' => array( |
46 | 46 | 'driver' => 'mongodb', |
47 | | - 'host' => array('server1', 'server2), |
| 47 | + 'host' => array('server1', 'server2'), |
48 | 48 | 'port' => 27017, |
49 | 49 | 'username' => 'username', |
50 | 50 | 'password' => 'password', |
@@ -414,6 +414,11 @@ You can remove an embedded document by using the `destroy()` method: |
414 | 414 | // or |
415 | 415 | $user->books()->destroy($book); |
416 | 416 |
|
| 417 | +If you want to add or remove embedded documents, without persistence, you can use the `associate` and `dissociate` methods. To write the changes to the database, save the parent object: |
| 418 | + |
| 419 | + $user->books()->associate($book); |
| 420 | + $user->save(); |
| 421 | + |
417 | 422 | Again, you may override the conventional local key by passing a second argument to the embedsMany method: |
418 | 423 |
|
419 | 424 | return $this->embedsMany('Book', 'local_key'); |
@@ -458,16 +463,23 @@ These expressions will be injected directly into the query. |
458 | 463 |
|
459 | 464 | User::whereRaw(array('age' => array('$gt' => 30, '$lt' => 40)))->get(); |
460 | 465 |
|
461 | | -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. |
| 466 | +You can also perform raw expressions on the internal MongoCollection object. If this is executed on the model class, it will return a collection of models. If this is executed on the query builder, it will return the original response. |
| 467 | + |
| 468 | + // Returns a collection of User models. |
| 469 | + $models = User::raw(function($collection) |
| 470 | + { |
| 471 | + return $collection->find(); |
| 472 | + }); |
462 | 473 |
|
463 | | - User::raw(function($collection) |
| 474 | + // Returns the original MongoCursor. |
| 475 | + $cursor = DB::collection('users')->raw(function($collection) |
464 | 476 | { |
465 | 477 | return $collection->find(); |
466 | 478 | }); |
467 | 479 |
|
468 | | -Or you can access the internal MongoCollection object directly: |
| 480 | +Optional: if you don't pass a closure to the raw method, the internal MongoCollection object will be accessible: |
469 | 481 |
|
470 | | - User::raw()->find(); |
| 482 | + $model = User::raw()->findOne(array('age' => array('$lt' => 18))); |
471 | 483 |
|
472 | 484 | The MongoClient and MongoDB objects can be accessed like this: |
473 | 485 |
|
|
0 commit comments