2121use function assert ;
2222use function count ;
2323use function current ;
24+ use function explode ;
2425use function implode ;
2526use function in_array ;
2627use function is_array ;
2728use function is_string ;
2829use function iterator_to_array ;
2930use function sort ;
3031use function sprintf ;
32+ use function str_contains ;
3133use function str_ends_with ;
3234use function substr ;
35+ use function trigger_error ;
3336use function usort ;
3437
38+ use const E_USER_DEPRECATED ;
39+
3540/** @property Connection $connection */
3641class Builder extends \Illuminate \Database \Schema \Builder
3742{
@@ -47,7 +52,7 @@ public function hasColumn($table, $column): bool
4752 }
4853
4954 /**
50- * Check if columns exists in the collection schema.
55+ * Check if columns exist in the collection schema.
5156 *
5257 * @param string $table
5358 * @param string[] $columns
@@ -134,12 +139,18 @@ public function drop($table)
134139 $ blueprint ->drop ();
135140 }
136141
137- /** @inheritdoc */
142+ /**
143+ * @inheritdoc
144+ *
145+ * Drops the entire database instead of deleting each collection individually.
146+ *
147+ * In MongoDB, dropping the whole database is much faster than dropping collections
148+ * one by one. The database will be automatically recreated when a new connection
149+ * writes to it.
150+ */
138151 public function dropAllTables ()
139152 {
140- foreach ($ this ->getAllCollections () as $ collection ) {
141- $ this ->drop ($ collection );
142- }
153+ $ this ->connection ->getDatabase ()->drop ();
143154 }
144155
145156 /** @param string|null $schema Database name */
@@ -148,7 +159,14 @@ public function getTables($schema = null)
148159 $ db = $ this ->connection ->getDatabase ($ schema );
149160 $ collections = [];
150161
151- foreach ($ db ->listCollectionNames () as $ collectionName ) {
162+ foreach ($ db ->listCollections () as $ collectionInfo ) {
163+ $ collectionName = $ collectionInfo ->getName ();
164+
165+ // Skip views, which don't support aggregate
166+ if ($ collectionInfo ->getType () === 'view ' ) {
167+ continue ;
168+ }
169+
152170 $ stats = $ db ->selectCollection ($ collectionName )->aggregate ([
153171 ['$collStats ' => ['storageStats ' => ['scale ' => 1 ]]],
154172 ['$project ' => ['storageStats.totalSize ' => 1 ]],
@@ -165,9 +183,37 @@ public function getTables($schema = null)
165183 ];
166184 }
167185
168- usort ($ collections , function ($ a , $ b ) {
169- return $ a ['name ' ] <=> $ b ['name ' ];
170- });
186+ usort ($ collections , fn ($ a , $ b ) => $ a ['name ' ] <=> $ b ['name ' ]);
187+
188+ return $ collections ;
189+ }
190+
191+ /** @param string|null $schema Database name */
192+ public function getViews ($ schema = null )
193+ {
194+ $ db = $ this ->connection ->getDatabase ($ schema );
195+ $ collections = [];
196+
197+ foreach ($ db ->listCollections () as $ collectionInfo ) {
198+ $ collectionName = $ collectionInfo ->getName ();
199+
200+ // Skip normal type collection
201+ if ($ collectionInfo ->getType () !== 'view ' ) {
202+ continue ;
203+ }
204+
205+ $ collections [] = [
206+ 'name ' => $ collectionName ,
207+ 'schema ' => $ db ->getDatabaseName (),
208+ 'schema_qualified_name ' => $ db ->getDatabaseName () . '. ' . $ collectionName ,
209+ 'size ' => null ,
210+ 'comment ' => null ,
211+ 'collation ' => null ,
212+ 'engine ' => null ,
213+ ];
214+ }
215+
216+ usort ($ collections , fn ($ a , $ b ) => $ a ['name ' ] <=> $ b ['name ' ]);
171217
172218 return $ collections ;
173219 }
@@ -203,7 +249,12 @@ public function getTableListing($schema = null, $schemaQualified = false)
203249
204250 public function getColumns ($ table )
205251 {
206- $ stats = $ this ->connection ->getDatabase ()->selectCollection ($ table )->aggregate ([
252+ $ db = null ;
253+ if (str_contains ($ table , '. ' )) {
254+ [$ db , $ table ] = explode ('. ' , $ table , 2 );
255+ }
256+
257+ $ stats = $ this ->connection ->getDatabase ($ db )->selectCollection ($ table )->aggregate ([
207258 // Sample 1,000 documents to get a representative sample of the collection
208259 ['$sample ' => ['size ' => 1_000 ]],
209260 // Convert each document to an array of fields
@@ -340,10 +391,14 @@ public function getCollection($name)
340391 /**
341392 * Get all of the collections names for the database.
342393 *
394+ * @deprecated
395+ *
343396 * @return array
344397 */
345398 protected function getAllCollections ()
346399 {
400+ trigger_error (sprintf ('Since mongodb/laravel-mongodb:5.4, Method "%s()" is deprecated without replacement. ' , __METHOD__ ), E_USER_DEPRECATED );
401+
347402 $ collections = [];
348403 foreach ($ this ->connection ->getDatabase ()->listCollections () as $ collection ) {
349404 $ collections [] = $ collection ->getName ();
0 commit comments