@@ -167,17 +167,118 @@ as in the preceding example, but the query is constructed by using the
167167List Collections
168168----------------
169169
170- To see information about each of the collections in a database, call the
171- ``listCollections()`` method.
170+ You can take either of the following actions to see information
171+ about the collections in a database:
172172
173- The following example accesses a database connection, then
174- calls the ``listCollections()`` method to retrieve information about the
175- collections in the database:
173+ - :ref:`laravel-list-coll-command`
174+ - :ref:`laravel-list-coll-methods`
175+
176+ .. _laravel-list-coll-command:
177+
178+ Run a Shell Command
179+ ~~~~~~~~~~~~~~~~~~~
180+
181+ You can list the collections in a database by running the following
182+ command in your shell from your project's root directory:
183+
184+ .. code-block:: bash
185+
186+ php artisan db:show
187+
188+ This command outputs information about the configured database and lists its
189+ collections under the ``Table`` header. For more information about the ``db:show``
190+ command, see `Laravel: New DB Commands <https://blog.laravel.com/laravel-new-db-commands-and-more>`__
191+ on the official Laravel blog.
192+
193+ .. _laravel-list-coll-methods:
194+
195+ Call Database or Schema Methods
196+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197+
198+ You can list the collections in a database by calling the following
199+ methods in your application:
200+
201+ - ``DB::listCollections()``: lists information about each collection by
202+ using the query builder
203+ - ``Schema::getTablesListing()``: lists the name of each collection by
204+ using the schema builder
205+ - ``Schema::getTables()``: lists the name and size of each collection by
206+ using the schema builder
207+
208+ .. note::
209+
210+ MongoDB is a schemaless database, so the preceding schema builder methods
211+ query the database data rather than the schema.
212+
213+ Example
214+ ```````
215+
216+ The following example accesses a database connection, then calls the
217+ ``listCollections()`` query builder method to retrieve information about
218+ the collections in the database:
176219
177220.. code-block:: php
178221
179222 $collections = DB::connection('mongodb')->getMongoDB()->listCollections();
180223
224+ List Collection Fields
225+ ----------------------
226+
227+ You can take either of the following actions to see information
228+ about each field in a collection:
229+
230+ - :ref:`laravel-list-fields-command`
231+ - :ref:`laravel-list-fields-methods`
232+
233+ .. _laravel-list-fields-command:
234+
235+ Run a Shell Command
236+ ~~~~~~~~~~~~~~~~~~~
237+
238+ You can see a list of fields in a collection by running the following
239+ command in your shell from your project's root directory:
240+
241+ .. code-block:: bash
242+
243+ php artisan db:table <collection name>
244+
245+ This command outputs each collection field and its corresponding data type
246+ under the ``Column`` header. For more information about the ``db:table``
247+ command, see `Laravel: New DB Commands <https://blog.laravel.com/laravel-new-db-commands-and-more>`__
248+ on the official Laravel blog.
249+
250+ .. _laravel-list-fields-methods:
251+
252+ Call Schema Methods
253+ ~~~~~~~~~~~~~~~~~~~
254+
255+ You can list the fields in a collection by calling the ``Schema::getColumns()``
256+ schema builder method in your application.
257+
258+ You can also use the following methods to return more information about the
259+ collection fields:
260+
261+ - ``Schema::hasColumn(string $<collection>, string $<field name>)``: checks if the specified field exists
262+ in at least one document
263+ - ``Schema::hasColumns(string $<collection>, string[] $<field names>)``: checks if each specified field exists
264+ in at least one document
265+
266+ .. note::
267+
268+ MongoDB is a schemaless database, so the preceding methods query the collection
269+ data rather than the database schema. If the specified collection doesn't exist
270+ or is empty, these methods return a value of ``false``.
271+
272+ Example
273+ ```````
274+
275+ The following example passes a collection name to the ``Schema::getColumns()``
276+ method to retrieve each field in the ``flowers`` collection:
277+
278+ .. code-block:: php
279+
280+ $fields = Schema::getColumns('flowers');
281+
181282Create and Drop Collections
182283---------------------------
183284
0 commit comments