Skip to content

Commit 09ef676

Browse files
DOCSP-7607: Delete multiple documents usage example (#10)
1 parent bcef6f2 commit 09ef676

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ignored first line
2+
const { MongoClient } = require("mongodb");
3+
4+
const uri =
5+
"mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
6+
7+
const client = new MongoClient(uri);
8+
9+
async function run() {
10+
try {
11+
await client.connect();
12+
13+
const database = client.db("sample_mflix");
14+
const collection = database.collection("movies");
15+
// Query for all movies with the title "Santa Claus"
16+
const query = { title: "Santa Claus" };
17+
18+
collection.deleteMany(query, options, function(err, r) {
19+
if (err) {
20+
console.log("Error: " + err.errmsg);
21+
} else {
22+
console.log("Deleted " + r.deletedCount + " documents");
23+
}
24+
});
25+
} finally {
26+
await client.close();
27+
}
28+
}
29+
run().catch(console.dir);

source/usage-examples.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ Usage Examples
66

77
:doc:`findOne </usage-examples/findOne>`
88
:doc:`find </usage-examples/find>`
9+
:doc:`deleteMany </usage-examples/deleteMany>`
910
:doc:`findOneAndDelete </usage-examples/findOneAndDelete>`
1011

12+
1113
.. toctree::
1214
:caption: Examples
1315

1416
/usage-examples/findOne
1517
/usage-examples/find
18+
/usage-examples/deleteMany
1619
/usage-examples/findOneAndDelete

source/usage-examples/deleteMany.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)