|
| 1 | +================= |
| 2 | +Delete a Document |
| 3 | +================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. _delete_guide_golang: |
| 14 | + |
| 15 | +Overview |
| 16 | +-------- |
| 17 | + |
| 18 | +In this guide, you can learn how to remove documents from your MongoDB |
| 19 | +collections using delete operations. |
| 20 | + |
| 21 | +Sample Data |
| 22 | +~~~~~~~~~~~ |
| 23 | + |
| 24 | +To run the example in this guide, load these documents into the |
| 25 | +``ratings`` collection of the ``tea`` database with the following |
| 26 | +snippet: |
| 27 | + |
| 28 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/delete.go |
| 29 | + :language: go |
| 30 | + :dedent: |
| 31 | + :start-after: begin insertDocs |
| 32 | + :end-before: end insertDocs |
| 33 | + |
| 34 | +.. tip:: Non-existent Database and Collections |
| 35 | + |
| 36 | + The driver automatically creates the necessary database and/or collection |
| 37 | + when you perform a write operation against them if they don't already |
| 38 | + exist. |
| 39 | + |
| 40 | +Each document contains a rating for a type of tea, which corresponds to |
| 41 | +the ``type`` and ``rating`` fields. |
| 42 | + |
| 43 | +Delete Operations |
| 44 | +----------------- |
| 45 | + |
| 46 | +Use **delete operations** to remove data from MongoDB. Delete operations |
| 47 | +consist of the following functions: |
| 48 | + |
| 49 | +- ``DeleteOne()``, which deletes *the first document* that matches the filter |
| 50 | +- ``DeleteMany()``, which deletes *all* documents that match the filter |
| 51 | + |
| 52 | +.. tip:: |
| 53 | + |
| 54 | + If one document matches your filter when running the ``DeleteMany()`` |
| 55 | + function, it's equivalent to running the ``DeleteOne()`` function. |
| 56 | + |
| 57 | +Parameters |
| 58 | +~~~~~~~~~~ |
| 59 | + |
| 60 | +The ``DeleteOne()`` and ``DeleteMany()`` functions expect you to pass a |
| 61 | +``Context`` type and a ``non-nil`` query filter specifying which |
| 62 | +documents to match. |
| 63 | + |
| 64 | +They both optionally take a ``DeleteOptions`` type as a third parameter, |
| 65 | +which represents options you can use to configure the delete operation. |
| 66 | +If you don't specify a ``DeleteOptions``, the driver uses the default |
| 67 | +values for each option. |
| 68 | + |
| 69 | +The ``DeleteOptions`` type allows you to configure options with the |
| 70 | +following functions: |
| 71 | + |
| 72 | +.. list-table:: |
| 73 | + :widths: 30 70 |
| 74 | + :header-rows: 1 |
| 75 | + |
| 76 | + * - Function |
| 77 | + - Description |
| 78 | + |
| 79 | + * - ``SetHint()`` |
| 80 | + - | The index to use to scan for documents to delete. |
| 81 | + | Default: ``nil`` |
| 82 | + |
| 83 | + * - ``SetCollation()`` |
| 84 | + - | The type of language collation to use when sorting results. |
| 85 | + | Default: ``nil`` |
| 86 | + |
| 87 | +Return Value |
| 88 | +~~~~~~~~~~~~ |
| 89 | + |
| 90 | +The ``DeleteOne()`` and ``DeleteMany()`` functions return a |
| 91 | +``DeleteResult`` type. This type contains the ``DeletedCount`` property, |
| 92 | +which states the number of documents deleted. If there are no matches to |
| 93 | +your filter, no document gets deleted and ``DeletedCount`` is ``0``. |
| 94 | + |
| 95 | +Example |
| 96 | +``````` |
| 97 | + |
| 98 | +The following example performs the following with the ``DeleteMany()`` |
| 99 | +function: |
| 100 | + |
| 101 | +- Matches and deletes documents where the ``rating`` is greater than ``8`` |
| 102 | +- Specifies the function to use the ``_id`` as the index |
| 103 | + |
| 104 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/delete.go |
| 105 | + :language: go |
| 106 | + :dedent: |
| 107 | + :start-after: begin deleteMany |
| 108 | + :end-before: end deleteMany |
| 109 | + |
| 110 | +After running the preceding example, the output resembles the following: |
| 111 | + |
| 112 | +.. code-block:: none |
| 113 | + :copyable: false |
| 114 | + |
| 115 | + Number of documents deleted: 2 |
| 116 | + |
| 117 | +.. tip:: |
| 118 | + |
| 119 | + If the preceding example used the ``DeleteOne()`` function instead of |
| 120 | + ``DeleteMany()``, the driver would delete the first of the two |
| 121 | + matched documents. |
| 122 | + |
| 123 | +Additional Information |
| 124 | +---------------------- |
| 125 | + |
| 126 | +For runnable examples of the delete operations, see the following usage |
| 127 | +examples: |
| 128 | + |
| 129 | +- :doc:`Delete a Document </usage-examples/deleteOne>` |
| 130 | +- :doc:`Delete Multiple Documents </usage-examples/deleteMany>` |
| 131 | + |
| 132 | +For more information about how the driver uses Context, see our guide on |
| 133 | +:doc:`Context </fundamentals/context>`. |
| 134 | + |
| 135 | +.. For more information about specifying hints, see our guide on <TODO: Indexes>. |
| 136 | +.. For more information about collations, see our guide on <TODO: Collations>. |
| 137 | + |
| 138 | +API Documentation |
| 139 | +~~~~~~~~~~~~~~~~~ |
| 140 | + |
| 141 | +For more information on any of the functions or types discussed in this |
| 142 | +guide, see the following API Documentation: |
| 143 | + |
| 144 | +- `DeleteOne() <{+api+}/mongo#Collection.DeleteOne>`__ |
| 145 | +- `DeleteMany() <{+api+}/mongo#Collection.DeleteMany>`__ |
| 146 | +- `DeleteOptions <{+api+}/options#DeleteOptions>`__ |
| 147 | +- `DeleteResult <{+api+}/mongo#DeleteResult>`__ |
0 commit comments