|
| 1 | +=================== |
| 2 | +Delete One Document |
| 3 | +=================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Overview |
| 8 | +-------- |
| 9 | + |
| 10 | +You can delete a single document in a collection with |
| 11 | +``collection.deleteOne()``. |
| 12 | +The ``deleteOne()`` method uses a query document that you provide |
| 13 | +to match only the subset of the documents in the collection that match |
| 14 | +the query. If you don't provide a query document (or if you provide an |
| 15 | +empty document), MongoDB matches all documents in the collection. |
| 16 | +However, only the first matched document is deleted. |
| 17 | + |
| 18 | +You can define additional query options using the |
| 19 | +``options`` object passed as the second parameter of the |
| 20 | +``deleteOne`` method. You can also pass a |
| 21 | +`callback method <https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#~deleteWriteOpCallback>`_ |
| 22 | +as an optional third parameter. For detailed reference documentation, see |
| 23 | +`collection.deleteOne() <https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#deleteOne>`_. |
| 24 | + |
| 25 | +``deleteOne()`` behaves in two different ways depending on |
| 26 | +whether or not a callback method is provided: |
| 27 | + |
| 28 | +- if no callback method is provided, ``deleteOne()`` returns a |
| 29 | + `Promise <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>`_ |
| 30 | + that resolves to an |
| 31 | + `Object <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object>`_ |
| 32 | + |
| 33 | +- if a callback method is provided, ``deleteOne()`` returns |
| 34 | + nothing, and instead passes the result object or error object to the |
| 35 | + provided callback method |
| 36 | + |
| 37 | +The `result object <http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#~deleteWriteOpResult>`_ |
| 38 | +contains several keys in the event of a successful execution. You can |
| 39 | +use the ``deletedCount`` key to check the number of documents deleted by |
| 40 | +the operation. Since ``deleteOne()`` can only delete a single document, |
| 41 | +``deletedCount`` can only have a value of ``0`` or ``1``. |
| 42 | + |
| 43 | +The error object contains ``errmsg``, a human-readable explanation of |
| 44 | +what caused the operation to fail. |
| 45 | + |
| 46 | +.. note:: |
| 47 | + |
| 48 | + If your application requires the deleted document after deletion, |
| 49 | + consider using the |
| 50 | + `collection.findOneAndDelete() <https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#findOneAndDelete>`_. |
| 51 | + method, which has a similar interface to ``deleteOne()`` but also |
| 52 | + returns the deleted document. |
| 53 | + |
| 54 | +Example |
| 55 | +------- |
| 56 | + |
| 57 | +The following snippet deletes a single document from the ``movies`` |
| 58 | +collection. It uses a **query document** that configures the query |
| 59 | +to match only movies with a title of type ``string``. |
| 60 | + |
| 61 | +.. literalinclude:: /code-snippets/usage-examples/deleteOne.js |
| 62 | + :language: javascript |
0 commit comments