@@ -22,19 +22,22 @@ Installation
22
22
23
23
Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php.php
24
24
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:
26
28
27
29
```
28
30
composer require jenssegers/mongodb
29
31
```
30
32
31
- ### Version Compatibility
33
+ ### Laravel version Compatibility
32
34
33
35
Laravel | Package
34
36
:---------|:----------
35
37
4.2.x | 2.0.x
36
38
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
38
41
39
42
And add the service provider in ` config/app.php ` :
40
43
@@ -73,31 +76,31 @@ Change your default database connection name in `app/config/database.php`:
73
76
And add a new mongodb connection:
74
77
75
78
``` php
76
- 'mongodb' => array(
79
+ 'mongodb' => [
77
80
'driver' => 'mongodb',
78
81
'host' => env('DB_HOST', 'localhost'),
79
82
'port' => env('DB_PORT', 27017),
80
83
'database' => env('DB_DATABASE', ''),
81
84
'username' => env('DB_USERNAME', ''),
82
85
'password' => env('DB_PASSWORD', ''),
83
- 'options' => array(
86
+ 'options' => [
84
87
'db' => 'admin' // sets the authentication database required by mongo 3
85
- )
86
- ) ,
88
+ ]
89
+ ] ,
87
90
```
88
91
89
92
You can connect to multiple servers or replica sets with the following configuration:
90
93
91
94
``` php
92
- 'mongodb' => array(
95
+ 'mongodb' => [
93
96
'driver' => 'mongodb',
94
- 'host' => array( 'server1', 'server2') ,
97
+ 'host' => [ 'server1', 'server2'] ,
95
98
'port' => env('DB_PORT', 27017),
96
99
'database' => env('DB_DATABASE', ''),
97
100
'username' => env('DB_USERNAME', ''),
98
101
'password' => env('DB_PASSWORD', ''),
99
- 'options' => array( 'replicaSet' => 'replicaSetName')
100
- ) ,
102
+ 'options' => [ 'replicaSet' => 'replicaSetName']
103
+ ] ,
101
104
```
102
105
103
106
Eloquent
@@ -135,7 +138,7 @@ class MyModel extends Eloquent {
135
138
}
136
139
```
137
140
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
139
142
140
143
### Optional: Alias
141
144
@@ -216,34 +219,6 @@ If you want to use this library with [Sentry](https://cartalyst.com/manual/sentr
216
219
217
220
The MongoDB session driver is available in a separate package, check out https://github.com/jenssegers/Laravel-MongoDB-Session
218
221
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
-
247
222
Examples
248
223
--------
249
224
@@ -282,15 +257,15 @@ $users = User::where('votes', '>', 100)->where('name', '=', 'John')->get();
282
257
** Using Where In With An Array**
283
258
284
259
``` php
285
- $users = User::whereIn('age', array( 16, 18, 20) )->get();
260
+ $users = User::whereIn('age', [ 16, 18, 20] )->get();
286
261
```
287
262
288
263
When using ` whereNotIn ` objects will be returned if the field is non existent. Combine with ` whereNotNull('age') ` to leave out those documents.
289
264
290
265
** Using Where Between**
291
266
292
267
``` php
293
- $users = User::whereBetween('votes', array( 1, 100) )->get();
268
+ $users = User::whereBetween('votes', [ 1, 100] )->get();
294
269
```
295
270
296
271
** Where null**
@@ -316,7 +291,7 @@ $users = User::skip(10)->take(5)->get();
316
291
Distinct requires a field for which to return the distinct values.
317
292
318
293
``` php
319
- $users = User::distinct()->get(array( 'name') );
294
+ $users = User::distinct()->get([ 'name'] );
320
295
// or
321
296
$users = User::distinct('name')->get();
322
297
```
@@ -343,7 +318,7 @@ $users = User::where('name', '=', 'John')->orWhere(function($query)
343
318
Selected columns that are not grouped will be aggregated with the $last function.
344
319
345
320
``` php
346
- $users = Users::groupBy('title')->get(array( 'title', 'name') );
321
+ $users = Users::groupBy('title')->get([ 'title', 'name'] );
347
322
```
348
323
349
324
** Aggregation**
@@ -388,8 +363,8 @@ $count = User->increment('age');
388
363
You may also specify additional columns to update:
389
364
390
365
``` 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'] );
393
368
```
394
369
395
370
** Soft deleting**
@@ -425,7 +400,7 @@ User::where('age', 'exists', true)->get();
425
400
Matches arrays that contain all elements specified in the query.
426
401
427
402
``` php
428
- User::where('roles', 'all', array( 'moderator', 'author') )->get();
403
+ User::where('roles', 'all', [ 'moderator', 'author'] )->get();
429
404
```
430
405
431
406
** Size**
@@ -469,7 +444,7 @@ User::where('age', 'type', 2)->get();
469
444
Performs a modulo operation on the value of a field and selects documents with a specified result.
470
445
471
446
``` php
472
- User::where('age', 'mod', array( 10, 0) )->get();
447
+ User::where('age', 'mod', [ 10, 0] )->get();
473
448
```
474
449
475
450
** Where**
@@ -491,7 +466,7 @@ $user->save();
491
466
You may also use the create method to save a new model in a single line:
492
467
493
468
``` php
494
- User::create(array( 'name' => 'John') );
469
+ User::create([ 'name' => 'John'] );
495
470
```
496
471
497
472
** Updating a model**
@@ -534,7 +509,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
534
509
535
510
class User extends Eloquent {
536
511
537
- protected $dates = array( 'birthday') ;
512
+ protected $dates = [ 'birthday'] ;
538
513
539
514
}
540
515
```
@@ -634,13 +609,13 @@ $user = $book->user;
634
609
Inserting and updating embedded models works similar to the ` hasMany ` relation:
635
610
636
611
``` php
637
- $book = new Book(array( 'title' => 'A Game of Thrones') );
612
+ $book = new Book([ 'title' => 'A Game of Thrones'] );
638
613
639
614
$user = User::first();
640
615
641
616
$book = $user->books()->save($book);
642
617
// or
643
- $book = $user->books()->create(array( 'title' => 'A Game of Thrones') )
618
+ $book = $user->books()->create([ 'title' => 'A Game of Thrones'] )
644
619
```
645
620
646
621
You can update embedded models using their ` save ` method (available since release 2.0.0):
@@ -723,13 +698,13 @@ $author = Book::first()->author;
723
698
Inserting and updating embedded models works similar to the ` hasOne ` relation:
724
699
725
700
``` php
726
- $author = new Author(array( 'name' => 'John Doe') );
701
+ $author = new Author([ 'name' => 'John Doe'] );
727
702
728
703
$book = Books::first();
729
704
730
705
$author = $book->author()->save($author);
731
706
// or
732
- $author = $book->author()->create(array( 'name' => 'John Doe') );
707
+ $author = $book->author()->create([ 'name' => 'John Doe'] );
733
708
```
734
709
735
710
You can update the embedded model using the ` save ` method (available since release 2.0.0):
@@ -744,7 +719,7 @@ $author->save();
744
719
You can replace the embedded model with a new model like this:
745
720
746
721
``` php
747
- $newAuthor = new Author(array( 'name' => 'Jane Doe') );
722
+ $newAuthor = new Author([ 'name' => 'Jane Doe'] );
748
723
$book->author()->save($newAuthor);
749
724
```
750
725
@@ -793,7 +768,7 @@ class Message extends Eloquent {
793
768
These expressions will be injected directly into the query.
794
769
795
770
``` php
796
- User::whereRaw(array( 'age' => array('$gt' => 30, '$lt' => 40) ))->get();
771
+ User::whereRaw([ 'age' => array('$gt' => 30, '$lt' => 40] ))->get();
797
772
```
798
773
799
774
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.
@@ -815,7 +790,7 @@ $cursor = DB::collection('users')->raw(function($collection)
815
790
Optional: if you don't pass a closure to the raw method, the internal MongoCollection object will be accessible:
816
791
817
792
``` php
818
- $model = User::raw()->findOne(array( 'age' => array('$lt' => 18) ));
793
+ $model = User::raw()->findOne([ 'age' => array('$lt' => 18] ));
819
794
```
820
795
821
796
The 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
841
816
842
817
``` php
843
818
DB::collection('users')->where('name', 'John')
844
- ->update($data, array( 'upsert' => true) );
819
+ ->update($data, [ 'upsert' => true] );
845
820
```
846
821
847
822
** Projections**
848
823
849
824
You can apply projections to your queries using the ` project ` method.
850
825
851
826
``` php
852
- DB::collection('items')->project(array( 'tags' => array('$slice' => 1) ))->get();
827
+ DB::collection('items')->project([ 'tags' => array('$slice' => 1] ))->get();
853
828
```
854
829
855
830
** Projections with Pagination**
856
831
857
832
``` php
858
833
$limit = 25;
859
- $projections = array( 'id', 'name') ;
834
+ $projections = [ 'id', 'name'] ;
860
835
DB::collection('items')->paginate($limit, $projections);
861
836
```
862
837
@@ -867,7 +842,7 @@ Add an items to an array.
867
842
868
843
``` php
869
844
DB::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'] );
871
846
```
872
847
873
848
If you don't want duplicate items, set the third parameter to ` true ` :
@@ -882,7 +857,7 @@ Remove an item from an array.
882
857
883
858
``` php
884
859
DB::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'] );
886
861
```
887
862
888
863
** Unset**
0 commit comments