Skip to content

Commit d3d894a

Browse files
committed
Document DSN configuration as preferred configuration method
1 parent 2809d43 commit d3d894a

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

README.md

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,47 +143,36 @@ Keep in mind that these traits are not yet supported:
143143

144144
Configuration
145145
-------------
146-
You can use MongoDB either as the main database, either as a side database. To do so, add a new `mongodb` connection to `config/database.php`:
146+
147+
To configure a new MongoDB connection, add a new connection entry to `config/database.php`:
147148

148149
```php
149150
'mongodb' => [
150151
'driver' => 'mongodb',
151-
'host' => env('DB_HOST', '127.0.0.1'),
152-
'port' => env('DB_PORT', 27017),
152+
'dsn' => env('DB_DSN'),
153153
'database' => env('DB_DATABASE', 'homestead'),
154-
'username' => env('DB_USERNAME', 'homestead'),
155-
'password' => env('DB_PASSWORD', 'secret'),
156-
'options' => [
157-
// here you can pass more settings to the Mongo Driver Manager
158-
// see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use
159-
160-
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
161-
],
162154
],
163155
```
164156

165-
For multiple servers or replica set configurations, set the host to an array and specify each server host:
157+
The `dsn` key contains the connection string used to connect to your MongoDB deployment. The format and available options are documented in the [MongoDB documentation](https://docs.mongodb.com/manual/reference/connection-string/).
158+
159+
Instead of using a connection string, you can also use the `host` and `port` configuration options to have the connection string created for you.
166160

167161
```php
168162
'mongodb' => [
169163
'driver' => 'mongodb',
170-
'host' => ['server1', 'server2', ...],
171-
...
164+
'host' => env('DB_HOST', '127.0.0.1'),
165+
'port' => env('DB_PORT', 27017),
166+
'database' => env('DB_DATABASE', 'homestead'),
167+
'username' => env('DB_USERNAME', 'homestead'),
168+
'password' => env('DB_PASSWORD', 'secret'),
172169
'options' => [
173-
'replicaSet' => 'rs0',
170+
'appname' => 'homestead',
174171
],
175172
],
176173
```
177174

178-
If you wish to use a connection string instead of full key-value params, you can set it so. Check the documentation on MongoDB's URI format: https://docs.mongodb.com/manual/reference/connection-string/
179-
180-
```php
181-
'mongodb' => [
182-
'driver' => 'mongodb',
183-
'dsn' => env('DB_DSN'),
184-
'database' => env('DB_DATABASE', 'homestead'),
185-
],
186-
```
175+
The `options` key in the connection configuration corresponds to the [`uriOptions` parameter](https://www.php.net/manual/en/mongodb-driver-manager.construct.php#mongodb-driver-manager.construct-urioptions).
187176

188177
Eloquent
189178
--------
@@ -223,7 +212,7 @@ class Book extends Model
223212
protected $primaryKey = 'id';
224213
}
225214

226-
// Mongo will also create _id, but the 'id' property will be used for primary key actions like find().
215+
// MongoDB will also create _id, but the 'id' property will be used for primary key actions like find().
227216
Book::create(['id' => 1, 'title' => 'The Fault in Our Stars']);
228217
```
229218

@@ -238,7 +227,7 @@ class Book extends Model
238227
}
239228
```
240229

241-
### Extending the Authenticable base model
230+
### Extending the Authenticatable base model
242231
This package includes a MongoDB Authenticatable Eloquent class `Jenssegers\Mongodb\Auth\User` that you can use to replace the default Authenticatable class `Illuminate\Foundation\Auth\User` for your `User` model.
243232

244233
```php

0 commit comments

Comments
 (0)