@@ -55,6 +55,7 @@ mongodb:
55
55
clients:
56
56
default:
57
57
uri: '%env(MONGODB_URI)%'
58
+ default_database: #...
58
59
uriOptions: #...
59
60
driverOptions: #...
60
61
` ` `
@@ -146,7 +147,7 @@ class MyService
146
147
}
147
148
` ` `
148
149
149
- An alternative to this is using the `AutowireDatabase` attribute, referencing the database name :
150
+ An alternative to this is using the `#[ AutowireDatabase] ` attribute, referencing the database name:
150
151
151
152
` ` ` php
152
153
use MongoDB\B undle\A ttribute\A utowireDatabase;
@@ -166,13 +167,13 @@ In the following example the database name is `myDatabase`, inferred from the pr
166
167
167
168
` ` ` php
168
169
use MongoDB\B undle\A ttribute\A utowireCollection;
169
- use MongoDB\C ollection ;
170
+ use MongoDB\D atabase ;
170
171
171
172
class MyService
172
173
{
173
174
public function __construct(
174
- #[AutowireCollection() ]
175
- private Collection $myDatabase,
175
+ #[AutowireDatabase ]
176
+ private Database $myDatabase,
176
177
) {}
177
178
}
178
179
` ` `
@@ -193,7 +194,7 @@ class MyService
193
194
` ` `
194
195
195
196
To inject a collection, you can either call the `selectCollection` method on a `Client` or `Database` instance.
196
- For convenience, the `AutowireCollection` attribute provides a quicker alternative :
197
+ For convenience, the `#[ AutowireCollection] ` attribute provides a quicker alternative:
197
198
198
199
` ` ` php
199
200
use MongoDB\B undle\A ttribute\A utowireCollection;
@@ -230,6 +231,7 @@ class MyService
230
231
` ` `
231
232
232
233
If you have more than one client defined, you can also reference the client :
234
+
233
235
` ` ` php
234
236
use MongoDB\B undle\A ttribute\A utowireCollection;
235
237
use MongoDB\C ollection;
@@ -245,3 +247,26 @@ class MyService
245
247
) {}
246
248
}
247
249
` ` `
250
+
251
+ By specifiying the `default_database` option in the configuration, you can omit the `database` option in the attribute :
252
+
253
+ ` ` ` diff
254
+ mongodb:
255
+ clients:
256
+ default:
257
+ uri: '%env(MONGODB_URI)%'
258
+ + default_database: 'myDtabase'
259
+ ` ` `
260
+
261
+ ` ` ` php
262
+ use MongoDB\B undle\A ttribute\A utowireCollection;
263
+ use MongoDB\C ollection;
264
+
265
+ class MyService
266
+ {
267
+ public function __construct(
268
+ #[AutowireCollection]
269
+ private Collection $myCollection,
270
+ ) {}
271
+ }
272
+ ` ` `
0 commit comments