MongoDBDatabase-dropCollection.txt 1.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
===================================
MongoDB\\Database::dropCollection()
===================================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16
.. phpmethod:: MongoDB\\Database::dropCollection()
17 18

   Drop a collection within the current database.
19

20
   .. code-block:: php
21

22
      function dropCollection($collectionName, array $options = []): array|object
23

24
   This method has the following parameters:
25

26
   .. include:: /includes/apiargs/MongoDBDatabase-method-dropCollection-param.rst
27 28 29

   The ``$options`` parameter supports the following options:

30 31
   .. include:: /includes/apiargs/MongoDBDatabase-method-dropCollection-option.rst

32 33
Return Values
-------------
34

35 36 37
An array or object with the result document of the :manual:`drop
</reference/command/drop>` command. The return type will depend on the
``typeMap`` option.
38

39 40 41 42 43 44 45
Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

46 47 48
Example
-------

49
The following example drops the ``users`` collection in the ``test`` database:
50 51 52

.. code-block:: php

53 54
   <?php

55
   $db = (new MongoDB\Client)->test;
56 57

   $result = $db->dropCollection('users');
58

59 60 61 62 63 64 65 66
   var_dump($result);

The output would then resemble::

   object(MongoDB\Model\BSONDocument)#8 (1) {
     ["storage":"ArrayObject":private]=>
     array(3) {
       ["ns"]=>
67
       string(10) "test.users"
68 69 70 71 72 73 74
       ["nIndexesWas"]=>
       int(1)
       ["ok"]=>
       float(1)
     }
   }

75 76
See Also
--------
77

78 79 80
- :phpmethod:`MongoDB\\Collection::drop()`
- :manual:`drop </reference/command/drop>` command reference in the MongoDB
  manual