|
| 1 | +===================== |
| 2 | +Delete Many Documents |
| 3 | +===================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Overview |
| 8 | +-------- |
| 9 | + |
| 10 | +You can delete several documents in a collection at once with |
| 11 | +``collection.deleteMany()``. |
| 12 | +The ``deleteMany()`` 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. All |
| 16 | +matched documents are deleted. While you can use ``deleteMany()`` to |
| 17 | +delete all documents in a collection, consider using |
| 18 | +`drop() <http://mongodb.github.io/node-mongodb-native/3.4/api/Collection.html#drop>`_ |
| 19 | +instead for better performance and clearer code. |
| 20 | + |
| 21 | +You can define additional query options using the ``options`` |
| 22 | +object passed as the second parameter of the ``deleteMany()`` method. |
| 23 | +You can also pass a |
| 24 | +`callback method <https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#~deleteWriteOpCallback>`_ |
| 25 | +as an optional third parameter. For detailed reference documentation, |
| 26 | +see |
| 27 | +`collection.deleteMany() <https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#deleteMany>`_. |
| 28 | + |
| 29 | +``deleteMany()`` behaves in two different ways depending on |
| 30 | +whether or not a callback method is provided: |
| 31 | + |
| 32 | +- if no callback method is provided, ``deleteMany()`` returns a |
| 33 | + `Promise <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>`_ |
| 34 | + that resolves to an |
| 35 | + `Object <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object>`_ |
| 36 | + |
| 37 | +- if a callback method is provided, ``deleteMany()`` returns |
| 38 | + nothing, and instead passes the result object or error object to the |
| 39 | + provided callback method |
| 40 | + |
| 41 | +The |
| 42 | +`result object <http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#~deleteWriteOpResult>`_ |
| 43 | +contains multiple keys in the event of a successful |
| 44 | +execution. You can use the ``deletedCount`` key to determine the number |
| 45 | +of documents deleted by the operation. |
| 46 | + |
| 47 | +The error object contains ``errmsg``, a human-readable explanation of |
| 48 | +what caused the operation to fail. |
| 49 | + |
| 50 | +Example |
| 51 | +------- |
| 52 | + |
| 53 | +The following snippet deletes multiple documents from the ``movies`` |
| 54 | +collection. It uses a **query document** that configures the query to |
| 55 | +match and delete only movies with the title "Santa Claus". |
| 56 | + |
| 57 | +.. literalinclude:: /code-snippets/usage-examples/deleteMany.js |
| 58 | + :language: javascript |
0 commit comments