@@ -55,6 +55,7 @@ mongodb:
5555 clients:
5656 default:
5757 uri: '%env(MONGODB_URI)%'
58+ default_database: #...
5859 uri_options: #...
5960 driver_options: #...
6061` ` `
@@ -146,7 +147,7 @@ class MyService
146147}
147148` ` `
148149
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:
150151
151152` ` ` php
152153use MongoDB\B undle\A ttribute\A utowireDatabase;
@@ -166,13 +167,13 @@ In the following example the database name is `myDatabase`, inferred from the pr
166167
167168` ` ` php
168169use MongoDB\B undle\A ttribute\A utowireCollection;
169- use MongoDB\C ollection ;
170+ use MongoDB\D atabase ;
170171
171172class MyService
172173{
173174 public function __construct(
174- #[AutowireCollection() ]
175- private Collection $myDatabase,
175+ #[AutowireDatabase ]
176+ private Database $myDatabase,
176177 ) {}
177178}
178179` ` `
@@ -193,7 +194,7 @@ class MyService
193194` ` `
194195
195196To 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:
197198
198199` ` ` php
199200use MongoDB\B undle\A ttribute\A utowireCollection;
@@ -230,6 +231,7 @@ class MyService
230231` ` `
231232
232233If you have more than one client defined, you can also reference the client :
234+
233235` ` ` php
234236use MongoDB\B undle\A ttribute\A utowireCollection;
235237use MongoDB\C ollection;
@@ -245,3 +247,26 @@ class MyService
245247 ) {}
246248}
247249` ` `
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