Skip to content

Commit bcef6f2

Browse files
DOCSP-7606: delete a single document usage example (#9)
1 parent 00911fb commit bcef6f2

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
16+
// Query for a movie that has a title of type string
17+
const query = { title: { $type: "string" } };
18+
19+
collection.deleteOne(query, options, function(error, result) {
20+
if (error) {
21+
console.log("Error: " + error.errmsg);
22+
} else {
23+
if (result.deletedCount == 1) {
24+
console.dir("Successfully deleted one document.");
25+
} else {
26+
console.log("No documents matched the query.");
27+
}
28+
}
29+
});
30+
} finally {
31+
await client.close();
32+
}
33+
}
34+
run().catch(console.dir);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
16+
// Query for a movie that has the title of type string
17+
const query = { title: { $type: "string" } };
18+
19+
const options = {
20+
// sort matched documents in ascending order by rating
21+
sort: { rating: 1 },
22+
// Include only the `title` and `fullplot` fields in the returned document
23+
projection: { _id: 0, title: 1, fullplot: 1 },
24+
};
25+
26+
collection.findOneAndDelete(query, options, function(error, result) {
27+
if (error) {
28+
console.log("Error: " + error.errmsg);
29+
} else {
30+
if (result.lastErrorObject.n == 1) {
31+
console.log("Deleted document:");
32+
console.dir(result.value);
33+
} else {
34+
console.log("No documents matched the query.");
35+
}
36+
}
37+
});
38+
} finally {
39+
await client.close();
40+
}
41+
}
42+
run().catch(console.dir);

source/usage-examples.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ Usage Examples
66

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

1011
.. toctree::
1112
:caption: Examples
1213

1314
/usage-examples/findOne
14-
/usage-examples/find
15+
/usage-examples/find
16+
/usage-examples/findOneAndDelete

source/usage-examples/deleteOne.txt

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

Comments
 (0)