|
| 1 | +.. _csharp-delete-guide: |
| 2 | + |
| 3 | +================ |
| 4 | +Delete Documents |
| 5 | +================ |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 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 | +The example in this guide use the ``restaurants`` collection |
| 25 | +from the ``sample_restaurants`` database. The documents in this |
| 26 | +collection are modeled by the following ``Restaurant`` class: |
| 27 | + |
| 28 | +.. literalinclude:: /includes/fundamentals/code-examples/crud/delete.cs |
| 29 | + :language: csharp |
| 30 | + :dedent: |
| 31 | + :start-after: start-model |
| 32 | + :end-before: end-model |
| 33 | + |
| 34 | +This collection is from the :atlas:`sample datasets </sample-data>` provided |
| 35 | +by Atlas. See the :ref:`<csharp-quickstart>` to learn how to create a free MongoDB cluster |
| 36 | +and load this sample data. |
| 37 | + |
| 38 | +Delete Operations |
| 39 | +----------------- |
| 40 | + |
| 41 | +Use delete operations to remove documents that match a **query filter**. |
| 42 | +The query filter determines which records are selected for deletion |
| 43 | +based on the criteria in :manual:`the query filter |
| 44 | +document</core/document/#query-filter-documents>`. You can perform |
| 45 | +delete operations in MongoDB with the following methods: |
| 46 | + |
| 47 | +- ``DeleteOne()``, which deletes *the first document* that matches the query filter |
| 48 | +- ``DeleteMany()``, which deletes *all documents* that match the query filter |
| 49 | + |
| 50 | +Delete One Document |
| 51 | +~~~~~~~~~~~~~~~~~~~ |
| 52 | + |
| 53 | +The following code shows how to use the asynchronous |
| 54 | +``DeleteOneAsync()`` method or the synchronous ``DeleteOne()`` method to |
| 55 | +delete one document. |
| 56 | + |
| 57 | +.. tabs:: |
| 58 | + |
| 59 | + .. tab:: Asynchronous |
| 60 | + :tabid: delete-one-async |
| 61 | + |
| 62 | + .. code-block:: csharp |
| 63 | + :copyable: true |
| 64 | + |
| 65 | + var result = await _restaurantsCollection.DeleteOneAsync(filter); |
| 66 | + |
| 67 | + .. tab:: Synchronous |
| 68 | + :tabid: delete-one-sync |
| 69 | + |
| 70 | + .. code-block:: csharp |
| 71 | + :copyable: true |
| 72 | + |
| 73 | + var result = _restaurantsCollection.DeleteOne(filter); |
| 74 | + |
| 75 | +Delete Multiple Documents |
| 76 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 77 | + |
| 78 | +The following code shows how to use the asynchronous |
| 79 | +``DeleteManyAsync()`` method or the synchronous ``DeleteMany()`` method to |
| 80 | +delete all matched documents. |
| 81 | + |
| 82 | +.. tabs:: |
| 83 | + |
| 84 | + .. tab:: Asynchronous |
| 85 | + :tabid: delete-many-async |
| 86 | + |
| 87 | + .. code-block:: csharp |
| 88 | + :copyable: true |
| 89 | + |
| 90 | + var result = await _restaurantsCollection.DeleteManyAsync(filter); |
| 91 | + |
| 92 | + .. tab:: Synchronous |
| 93 | + :tabid: delete-many-sync |
| 94 | + |
| 95 | + .. code-block:: csharp |
| 96 | + :copyable: true |
| 97 | + |
| 98 | + var result = _restaurantsCollection.DeleteMany(filter); |
| 99 | + |
| 100 | +.. tip:: |
| 101 | + |
| 102 | + Find runnable examples using these methods under :ref:`additional |
| 103 | + information <additional-info>`. |
| 104 | + |
| 105 | +Parameters |
| 106 | +~~~~~~~~~~ |
| 107 | + |
| 108 | +The ``DeleteOne()`` and ``DeleteMany()`` methods require you to pass a |
| 109 | +query filter specifying which documents to match. More information |
| 110 | +on how to construct a query filter is available in :manual:`the Query Documents |
| 111 | +tutorial</tutorial/query-documents/>`. |
| 112 | + |
| 113 | +Both methods optionally take a ``DeleteOptions`` type as an additional parameter, |
| 114 | +which represents options you can use to configure the delete operation. |
| 115 | +If you don't specify any ``DeleteOptions`` properties, the driver does |
| 116 | +not customize the delete operation. |
| 117 | + |
| 118 | +The ``DeleteOptions`` type allows you to configure options with the |
| 119 | +following properties: |
| 120 | + |
| 121 | +.. list-table:: |
| 122 | + :widths: 30 70 |
| 123 | + :header-rows: 1 |
| 124 | + |
| 125 | + * - Property |
| 126 | + - Description |
| 127 | + |
| 128 | + * - ``Collation`` |
| 129 | + - | Gets or sets the type of language collation to use when sorting |
| 130 | + results. See :manual:`the delete |
| 131 | + statements</reference/command/delete/#std-label-deletes-array-collation>` |
| 132 | + for more information. |
| 133 | + |
| 134 | + * - ``Comment`` |
| 135 | + - | Gets or sets the comment for the operation. See :manual:`the delete command |
| 136 | + fields</reference/command/delete/#command-fields>` |
| 137 | + for more information. |
| 138 | + |
| 139 | + * - ``Hint`` |
| 140 | + - | Gets or sets the index to use to scan for documents. See :manual:`the delete |
| 141 | + statements</reference/command/delete/#std-label-deletes-array-hint>` |
| 142 | + for more information. |
| 143 | + |
| 144 | + * - ``Let`` |
| 145 | + - | Gets or sets the let document. See :manual:`the delete command |
| 146 | + fields</reference/command/delete/#command-fields>` |
| 147 | + for more information. |
| 148 | + |
| 149 | +Example |
| 150 | +~~~~~~~ |
| 151 | + |
| 152 | +The following code uses the ``DeleteMany()`` method to search on the |
| 153 | +"borough_1" index and delete all documents where the ``address.street`` |
| 154 | +field value includes the phrase "Pearl Street": |
| 155 | + |
| 156 | +.. io-code-block:: |
| 157 | + :copyable: true |
| 158 | + |
| 159 | + .. input:: |
| 160 | + :language: csharp |
| 161 | + |
| 162 | + var filter = Builders<Restaurant>.Filter |
| 163 | + .Regex("address.street", "Pearl Street"); |
| 164 | + |
| 165 | + DeleteOptions opts = new DeleteOptions { Hint = "borough_1" }; |
| 166 | + |
| 167 | + WriteLine("Deleting documents..."); |
| 168 | + var result = _restaurantsCollection.DeleteMany(filter, opts); |
| 169 | + |
| 170 | + WriteLine($"Deleted documents: {result.DeletedCount}"); |
| 171 | + WriteLine($"Result acknowledged? {result.IsAcknowledged}"); |
| 172 | + |
| 173 | + .. output:: |
| 174 | + :language: none |
| 175 | + :visible: false |
| 176 | + |
| 177 | + Deleting documents... |
| 178 | + Deleted documents: 26 |
| 179 | + Result acknowledged? True |
| 180 | + |
| 181 | +.. tip:: |
| 182 | + |
| 183 | + If the preceding example used the ``DeleteOne()`` method instead of |
| 184 | + ``DeleteMany()``, the driver would delete the first of the 26 |
| 185 | + matched documents. |
| 186 | + |
| 187 | +Return Value |
| 188 | +~~~~~~~~~~~~ |
| 189 | + |
| 190 | +The ``DeleteOne()`` and ``DeleteMany()`` methods return a |
| 191 | +``DeleteResult`` type. This type contains the ``DeletedCount`` property, |
| 192 | +which indicates the number of documents deleted, and the |
| 193 | +``IsAcknowledged`` property, which indicates if the result is |
| 194 | +acknowledged. If the query filter does not match any documents, no documents |
| 195 | +are deleted and ``DeletedCount`` is 0. |
| 196 | + |
| 197 | +.. _additional-info: |
| 198 | + |
| 199 | +Additional Information |
| 200 | +---------------------- |
| 201 | + |
| 202 | +For runnable examples of the delete operations, see the following usage |
| 203 | +examples: |
| 204 | + |
| 205 | +- :ref:`csharp-delete-one` |
| 206 | +- :ref:`csharp-delete-many` |
| 207 | + |
| 208 | +.. To learn more about performing the operations mentioned, see the |
| 209 | +.. following guides: |
| 210 | + |
| 211 | +.. TODO create specify a query |
| 212 | +.. - :ref:`csharp-query-document` |
| 213 | + |
| 214 | +API Documentation |
| 215 | +~~~~~~~~~~~~~~~~~ |
| 216 | + |
| 217 | +To learn more about any of the methods or types discussed in this |
| 218 | +guide, see the following API Documentation: |
| 219 | + |
| 220 | +- `DeleteOne() <{+api-root+}/Overload_MongoDB_Driver_IMongoCollectionExtensions_DeleteOne.htm>`__ |
| 221 | +- `DeleteOneAsync() <{+api-root+}/Overload_MongoDB_Driver_IMongoCollectionExtensions_DeleteOneAsync.htm>`__ |
| 222 | +- `DeleteMany() <{+api-root+}/Overload_MongoDB_Driver_IMongoCollectionExtensions_DeleteMany.htm>`__ |
| 223 | +- `DeleteManyAsync() <{+api-root+}/Overload_MongoDB_Driver_IMongoCollectionExtensions_DeleteManyAsync.htm>`__ |
| 224 | +- `DeleteOptions <{+api-root+}/T_MongoDB_Driver_DeleteOptions.htm>`__ |
| 225 | +- `DeleteResult <{+api-root+}/Properties_T_MongoDB_Driver_DeleteResult.htm>`__ |
0 commit comments