@@ -22,19 +22,22 @@ Installation
2222
2323Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php.php
2424
25- For Laravel 5, install the latest stable version using composer:
25+ ** WARNING** : The old mongo PHP driver is not supported anymore in versions >= 3.0.
26+
27+ Installation using composer:
2628
2729```
2830composer require jenssegers/mongodb
2931```
3032
31- ### Version Compatibility
33+ ### Laravel version Compatibility
3234
3335 Laravel | Package
3436:---------|:----------
3537 4.2.x | 2.0.x
3638 5.0.x | 2.1.x
37- 5.1.x | 2.2.x
39+ 5.1.x | 2.2.x or 3.0.x
40+ 5.2.x | 2.2.x or 3.0.x
3841
3942And add the service provider in ` config/app.php ` :
4043
@@ -73,31 +76,31 @@ Change your default database connection name in `app/config/database.php`:
7376And add a new mongodb connection:
7477
7578``` php
76- 'mongodb' => array(
79+ 'mongodb' => [
7780 'driver' => 'mongodb',
7881 'host' => env('DB_HOST', 'localhost'),
7982 'port' => env('DB_PORT', 27017),
8083 'database' => env('DB_DATABASE', ''),
8184 'username' => env('DB_USERNAME', ''),
8285 'password' => env('DB_PASSWORD', ''),
83- 'options' => array(
86+ 'options' => [
8487 'db' => 'admin' // sets the authentication database required by mongo 3
85- )
86- ) ,
88+ ]
89+ ] ,
8790```
8891
8992You can connect to multiple servers or replica sets with the following configuration:
9093
9194``` php
92- 'mongodb' => array(
95+ 'mongodb' => [
9396 'driver' => 'mongodb',
94- 'host' => array( 'server1', 'server2') ,
97+ 'host' => [ 'server1', 'server2'] ,
9598 'port' => env('DB_PORT', 27017),
9699 'database' => env('DB_DATABASE', ''),
97100 'username' => env('DB_USERNAME', ''),
98101 'password' => env('DB_PASSWORD', ''),
99- 'options' => array( 'replicaSet' => 'replicaSetName')
100- ) ,
102+ 'options' => [ 'replicaSet' => 'replicaSetName']
103+ ] ,
101104```
102105
103106Eloquent
@@ -135,7 +138,7 @@ class MyModel extends Eloquent {
135138}
136139```
137140
138- Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
141+ Everything else (should) work just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
139142
140143### Optional: Alias
141144
@@ -216,34 +219,6 @@ If you want to use this library with [Sentry](https://cartalyst.com/manual/sentr
216219
217220The MongoDB session driver is available in a separate package, check out https://github.com/jenssegers/Laravel-MongoDB-Session
218221
219- Troubleshooting
220- ---------------
221-
222- #### Class 'MongoClient' not found in ...
223-
224- The ` MongoClient ` class is part of the MongoDB PHP driver. Usually, this error means that you forgot to install, or did not install this driver correctly. You can find installation instructions for this driver at http://php.net/manual/en/mongo.installation.php .
225-
226- To check if you have installed the driver correctly, run the following command:
227-
228- ``` sh
229- $ php -i | grep ' Mongo'
230- MongoDB Support => enabled
231- ```
232-
233- #### Argument 2 passed to Illuminate\Database\Query\Builder::__ construct() must be an instance of Illuminate\Database\Query\Grammars\Grammar, null given
234-
235- To solve this, you will need to check two things. First check if your model is extending the correct class; this class should be ` Jenssegers\Mongodb\Eloquent\Model ` . Secondly, check if your model is using a MongoDB connection. If you did not change the default database connection in your database configuration file, you need to specify the MongoDB enabled connection. This is what your class should look like if you did not set up an alias and change the default database connection:
236-
237- ``` php
238- use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
239-
240- class User extends Eloquent {
241-
242- protected $connection = 'mongodb';
243-
244- }
245- ```
246-
247222Examples
248223--------
249224
@@ -282,15 +257,15 @@ $users = User::where('votes', '>', 100)->where('name', '=', 'John')->get();
282257** Using Where In With An Array**
283258
284259``` php
285- $users = User::whereIn('age', array( 16, 18, 20) )->get();
260+ $users = User::whereIn('age', [ 16, 18, 20] )->get();
286261```
287262
288263When using ` whereNotIn ` objects will be returned if the field is non existent. Combine with ` whereNotNull('age') ` to leave out those documents.
289264
290265** Using Where Between**
291266
292267``` php
293- $users = User::whereBetween('votes', array( 1, 100) )->get();
268+ $users = User::whereBetween('votes', [ 1, 100] )->get();
294269```
295270
296271** Where null**
@@ -316,7 +291,7 @@ $users = User::skip(10)->take(5)->get();
316291Distinct requires a field for which to return the distinct values.
317292
318293``` php
319- $users = User::distinct()->get(array( 'name') );
294+ $users = User::distinct()->get([ 'name'] );
320295// or
321296$users = User::distinct('name')->get();
322297```
@@ -343,7 +318,7 @@ $users = User::where('name', '=', 'John')->orWhere(function($query)
343318Selected columns that are not grouped will be aggregated with the $last function.
344319
345320``` php
346- $users = Users::groupBy('title')->get(array( 'title', 'name') );
321+ $users = Users::groupBy('title')->get([ 'title', 'name'] );
347322```
348323
349324** Aggregation**
@@ -388,8 +363,8 @@ $count = User->increment('age');
388363You may also specify additional columns to update:
389364
390365``` php
391- User::where('age', '29')->increment('age', 1, array( 'group' => 'thirty something') );
392- User::where('bmi', 30)->decrement('bmi', 1, array( 'category' => 'overweight') );
366+ User::where('age', '29')->increment('age', 1, [ 'group' => 'thirty something'] );
367+ User::where('bmi', 30)->decrement('bmi', 1, [ 'category' => 'overweight'] );
393368```
394369
395370** Soft deleting**
@@ -425,7 +400,7 @@ User::where('age', 'exists', true)->get();
425400Matches arrays that contain all elements specified in the query.
426401
427402``` php
428- User::where('roles', 'all', array( 'moderator', 'author') )->get();
403+ User::where('roles', 'all', [ 'moderator', 'author'] )->get();
429404```
430405
431406** Size**
@@ -469,7 +444,7 @@ User::where('age', 'type', 2)->get();
469444Performs a modulo operation on the value of a field and selects documents with a specified result.
470445
471446``` php
472- User::where('age', 'mod', array( 10, 0) )->get();
447+ User::where('age', 'mod', [ 10, 0] )->get();
473448```
474449
475450** Where**
@@ -491,7 +466,7 @@ $user->save();
491466You may also use the create method to save a new model in a single line:
492467
493468``` php
494- User::create(array( 'name' => 'John') );
469+ User::create([ 'name' => 'John'] );
495470```
496471
497472** Updating a model**
@@ -534,7 +509,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
534509
535510class User extends Eloquent {
536511
537- protected $dates = array( 'birthday') ;
512+ protected $dates = [ 'birthday'] ;
538513
539514}
540515```
@@ -634,13 +609,13 @@ $user = $book->user;
634609Inserting and updating embedded models works similar to the ` hasMany ` relation:
635610
636611``` php
637- $book = new Book(array( 'title' => 'A Game of Thrones') );
612+ $book = new Book([ 'title' => 'A Game of Thrones'] );
638613
639614$user = User::first();
640615
641616$book = $user->books()->save($book);
642617// or
643- $book = $user->books()->create(array( 'title' => 'A Game of Thrones') )
618+ $book = $user->books()->create([ 'title' => 'A Game of Thrones'] )
644619```
645620
646621You can update embedded models using their ` save ` method (available since release 2.0.0):
@@ -723,13 +698,13 @@ $author = Book::first()->author;
723698Inserting and updating embedded models works similar to the ` hasOne ` relation:
724699
725700``` php
726- $author = new Author(array( 'name' => 'John Doe') );
701+ $author = new Author([ 'name' => 'John Doe'] );
727702
728703$book = Books::first();
729704
730705$author = $book->author()->save($author);
731706// or
732- $author = $book->author()->create(array( 'name' => 'John Doe') );
707+ $author = $book->author()->create([ 'name' => 'John Doe'] );
733708```
734709
735710You can update the embedded model using the ` save ` method (available since release 2.0.0):
@@ -744,7 +719,7 @@ $author->save();
744719You can replace the embedded model with a new model like this:
745720
746721``` php
747- $newAuthor = new Author(array( 'name' => 'Jane Doe') );
722+ $newAuthor = new Author([ 'name' => 'Jane Doe'] );
748723$book->author()->save($newAuthor);
749724```
750725
@@ -793,7 +768,7 @@ class Message extends Eloquent {
793768These expressions will be injected directly into the query.
794769
795770``` php
796- User::whereRaw(array( 'age' => array('$gt' => 30, '$lt' => 40) ))->get();
771+ User::whereRaw([ 'age' => array('$gt' => 30, '$lt' => 40] ))->get();
797772```
798773
799774You 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.
@@ -815,7 +790,7 @@ $cursor = DB::collection('users')->raw(function($collection)
815790Optional: if you don't pass a closure to the raw method, the internal MongoCollection object will be accessible:
816791
817792``` php
818- $model = User::raw()->findOne(array( 'age' => array('$lt' => 18) ));
793+ $model = User::raw()->findOne([ 'age' => array('$lt' => 18] ));
819794```
820795
821796The internal MongoClient and MongoDB objects can be accessed like this:
@@ -841,22 +816,22 @@ Update or insert a document. Additional options for the update method are passed
841816
842817``` php
843818DB::collection('users')->where('name', 'John')
844- ->update($data, array( 'upsert' => true) );
819+ ->update($data, [ 'upsert' => true] );
845820```
846821
847822** Projections**
848823
849824You can apply projections to your queries using the ` project ` method.
850825
851826``` php
852- DB::collection('items')->project(array( 'tags' => array('$slice' => 1) ))->get();
827+ DB::collection('items')->project([ 'tags' => array('$slice' => 1] ))->get();
853828```
854829
855830** Projections with Pagination**
856831
857832``` php
858833$limit = 25;
859- $projections = array( 'id', 'name') ;
834+ $projections = [ 'id', 'name'] ;
860835DB::collection('items')->paginate($limit, $projections);
861836```
862837
@@ -867,7 +842,7 @@ Add an items to an array.
867842
868843``` php
869844DB::collection('users')->where('name', 'John')->push('items', 'boots');
870- DB::collection('users')->where('name', 'John')->push('messages', array( 'from' => 'Jane Doe', 'message' => 'Hi John') );
845+ DB::collection('users')->where('name', 'John')->push('messages', [ 'from' => 'Jane Doe', 'message' => 'Hi John'] );
871846```
872847
873848If you don't want duplicate items, set the third parameter to ` true ` :
@@ -882,7 +857,7 @@ Remove an item from an array.
882857
883858``` php
884859DB::collection('users')->where('name', 'John')->pull('items', 'boots');
885- DB::collection('users')->where('name', 'John')->pull('messages', array( 'from' => 'Jane Doe', 'message' => 'Hi John') );
860+ DB::collection('users')->where('name', 'John')->pull('messages', [ 'from' => 'Jane Doe', 'message' => 'Hi John'] );
886861```
887862
888863** Unset**
0 commit comments