|
| 1 | +================= |
| 2 | +Delete a Document |
| 3 | +================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Overview |
| 8 | +-------- |
| 9 | + |
| 10 | +You can remove documents by passing a query filter to the |
| 11 | +:java-docs:`deleteOne() </apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#deleteOne(org.bson.conversions.Bson)>`, |
| 12 | +:java-docs:`deleteMany() </apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#deleteMany(org.bson.conversions.Bson)>` |
| 13 | +or :java-docs:`findOneAndDelete() </apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#findOneAndDelete(org.bson.conversions.Bson)>` methods. |
| 14 | + |
| 15 | +The ``deleteOne()`` method deletes a single document. If the query |
| 16 | +filter matches more than one document, the method will remove the first |
| 17 | +occurrence of a match in the collection. |
| 18 | + |
| 19 | +The ``deleteMany()`` method deletes all documents that match the query |
| 20 | +filter. |
| 21 | + |
| 22 | +The ``findOneAndDelete()`` method atomically finds and deletes the first |
| 23 | +occurrence of a match in the collection. |
| 24 | + |
| 25 | +To specify a collation or hint an index, use |
| 26 | +:java-docs:`DeleteOptions </apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteOptions.html>` |
| 27 | +as a second parameter to the ``deleteOne()`` and ``deleteMany()`` methods. |
| 28 | + |
| 29 | +To specify a collation, hint an index, specify sort order, or specify a |
| 30 | +projection on the returned document, use |
| 31 | +:java-docs:`FindOneAndDeleteOptions </apidocs/mongodb-driver-core/com/mongodb/client/model/FindOneAndDeleteOptions.html>` |
| 32 | +as the second parameter to the ``findOneAndDelete()`` method. |
| 33 | + |
| 34 | +Examples |
| 35 | +-------- |
| 36 | + |
| 37 | +The following examples are about a paint store that sells eight different |
| 38 | +colors of paint. The store had their annual online sale resulting in the |
| 39 | +following documents in their ``paint_inventory`` collection: |
| 40 | + |
| 41 | +.. code-block:: json |
| 42 | + |
| 43 | + { "_id": 1, "color": "red", "qty": 5 } |
| 44 | + { "_id": 2, "color": "purple", "qty": 8 } |
| 45 | + { "_id": 3, "color": "blue", "qty": 0 } |
| 46 | + { "_id": 4, "color": "white", "qty": 0 } |
| 47 | + { "_id": 5, "color": "yellow", "qty": 6 } |
| 48 | + { "_id": 6, "color": "pink", "qty": 0 } |
| 49 | + { "_id": 7, "color": "green", "qty": 0 } |
| 50 | + { "_id": 8, "color": "black", "qty": 8 } |
| 51 | + |
| 52 | +``deleteMany()`` |
| 53 | +```````````````` |
| 54 | + |
| 55 | +The paint store website displays all documents in the |
| 56 | +``paint_inventory`` collection. To reduce customer confusion, the store |
| 57 | +wants to remove the colors that are out of stock. |
| 58 | + |
| 59 | +To remove the out of stock colors, query the ``paint_inventory`` |
| 60 | +collection where the ``qty`` is ``0`` and pass the query to the |
| 61 | +``deleteMany()`` method: |
| 62 | + |
| 63 | +.. literalinclude:: /includes/fundamentals/code-snippets/Delete.java |
| 64 | + :language: java |
| 65 | + :dedent: |
| 66 | + :start-after: begin deleteManyExample |
| 67 | + :end-before: end deleteManyExample |
| 68 | + |
| 69 | +The following shows the documents remaining in the ``paint_inventory`` |
| 70 | +collection: |
| 71 | + |
| 72 | +.. code-block:: json |
| 73 | + |
| 74 | + { "_id": 1, "color": "red", "qty": 5 } |
| 75 | + { "_id": 2, "color": "purple", "qty": 8 } |
| 76 | + { "_id": 5, "color": "yellow", "qty": 6 } |
| 77 | + { "_id": 8, "color": "black", "qty": 8 } |
| 78 | + |
| 79 | +``deleteOne()`` |
| 80 | +``````````````` |
| 81 | + |
| 82 | +The store is donating the remaining quantity of their yellow paint. This |
| 83 | +means that the ``qty`` for yellow is now ``0`` and we need to remove yellow |
| 84 | +from the collection. |
| 85 | + |
| 86 | +To remove yellow, query the ``paint_inventory`` collection where the |
| 87 | +``color`` is ``"yellow"`` and pass the query to the ``deleteOne()`` |
| 88 | +method: |
| 89 | + |
| 90 | +.. literalinclude:: /includes/fundamentals/code-snippets/Delete.java |
| 91 | + :language: java |
| 92 | + :dedent: |
| 93 | + :start-after: begin deleteOneExample |
| 94 | + :end-before: end deleteOneExample |
| 95 | + |
| 96 | +The following shows the documents remaining in the ``paint_inventory`` |
| 97 | +collection: |
| 98 | + |
| 99 | +.. code-block:: json |
| 100 | + |
| 101 | + { "_id": 1, "color": "red", "qty": 5 } |
| 102 | + { "_id": 2, "color": "purple", "qty": 8 } |
| 103 | + { "_id": 8, "color": "black", "qty": 8 } |
| 104 | + |
| 105 | +``findOneAndDelete()`` |
| 106 | +`````````````````````` |
| 107 | + |
| 108 | +The store would like to raffle the remaining quantity of purple paint |
| 109 | +and remove purple from the ``paint_inventory`` collection. |
| 110 | + |
| 111 | +To pick a color, query the ``paint_inventory`` collection where the |
| 112 | +``color`` is ``"purple"`` and pass the query to the ``findOneAndDelete()`` |
| 113 | +method: |
| 114 | + |
| 115 | +.. literalinclude:: /includes/fundamentals/code-snippets/Delete.java |
| 116 | + :language: java |
| 117 | + :dedent: |
| 118 | + :start-after: begin findOneAndDeleteExample |
| 119 | + :end-before: end findOneAndDeleteExample |
| 120 | + |
| 121 | +Unlike the other delete methods, ``findOneAndDelete()`` returns the |
| 122 | +deleted document: |
| 123 | + |
| 124 | +.. code-block:: json |
| 125 | + |
| 126 | + { "_id": 2, "color": "purple", "qty": 8 } |
| 127 | + |
| 128 | +.. note:: |
| 129 | + |
| 130 | + If there are no matches to your query filter, no document gets |
| 131 | + deleted and the method returns null. |
| 132 | + |
| 133 | +The following shows the documents remaining in the ``paint_inventory`` |
| 134 | +collection: |
| 135 | + |
| 136 | +.. code-block:: json |
| 137 | + |
| 138 | + { "_id": 1, "color": "red", "qty": 5 } |
| 139 | + { "_id": 8, "color": "black", "qty": 8 } |
| 140 | + |
| 141 | +See the following documentation for more information about the deleting |
| 142 | +a document: |
| 143 | + |
| 144 | +- :manual:`db.collection.deleteOne() </reference/method/db.collection.deleteMany/>` |
| 145 | +- :manual:`db.collection.deleteMany() </reference/method/db.collection.deleteOne/>` |
| 146 | +- :manual:`db.collection.findOneAndDelete() </reference/method/db.collection.findOneAndDelete/>` |
0 commit comments