Skip to content

Commit 672eb21

Browse files
authored
DOCSP-7609: adds countDocuments and estimatedDocumentCount examples (#18)
* DOCSP-7609: adds countDocuments and estimatedDocumentCount exs * DOCSP-7609: style fixes + use Promise not callback
1 parent 8179b3a commit 672eb21

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ignored first line
2+
const { MongoClient } = require("mongodb");
3+
4+
// Replace the following with your MongoDB deployment's connection
5+
// string.
6+
const uri =
7+
"mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
8+
9+
const client = new MongoClient(uri, {
10+
useNewUrlParser: true,
11+
useUnifiedTopology: true,
12+
});
13+
14+
async function run() {
15+
try {
16+
await client.connect();
17+
18+
const database = client.db("sample_mflix");
19+
const collection = database.collection("movies");
20+
21+
// Estimate the total number of documents in the collection
22+
// and print out the count.
23+
var estimate = await collection.estimatedDocumentCount();
24+
console.log(
25+
"Estimated number of documents in the movies collection: " + estimate,
26+
);
27+
28+
// Query for movies from Canada.
29+
const query = { countries: "Canada" };
30+
31+
// Find the number of documents that match the specified
32+
// query, (i.e. with "Canada" as a value in the "countries" field)
33+
// and print out the count.
34+
var countCanada = await collection.countDocuments(query);
35+
console.log("Number of movies from Canada: " + countCanada);
36+
} finally {
37+
await client.close();
38+
}
39+
}
40+
run().catch(console.dir);

source/usage-examples.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Usage Examples
55
.. default-domain:: mongodb
66

77
:doc:`bulkWrite </usage-examples/bulkWrite>`
8+
:doc:`countDocuments and estimatedDocumentCount </usage-examples/count>`
89
:doc:`deleteMany </usage-examples/deleteMany>`
910
:doc:`insertOne </usage-examples/insertOne>`
1011
:doc:`deleteOne </usage-examples/deleteOne>`
@@ -17,6 +18,7 @@ Usage Examples
1718
:caption: Examples
1819

1920
/usage-examples/bulkWrite
21+
/usage-examples/count
2022
/usage-examples/deleteMany
2123
/usage-examples/deleteOne
2224
/usage-examples/insertOne

source/usage-examples/count.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
=============================================
2+
Count the Number of Documents in a Collection
3+
=============================================
4+
5+
.. default-domain:: mongodb
6+
7+
Overview
8+
--------
9+
10+
The Node.js driver provides two methods for counting
11+
documents in a collection:
12+
13+
- :node-api:`collection.countDocuments()
14+
</Collection.html#countDocuments>` returns the number of documents in
15+
the collection that match the specified query. If you specify a empty
16+
query document, ``countDocuments()`` returns the total number of
17+
documents in the collection.
18+
19+
- :node-api:`collection.estimatedDocumentCount
20+
</Collection.html#estimatedDocumentCount>` returns an
21+
**estimation** of the number of documents in the collection based on
22+
collection metadata.
23+
24+
``estimatedDocumentCount()`` is faster than ``countDocuments()`` because
25+
the estimation is based on the collection's metadata rather than scanning
26+
the collection. In contrast,
27+
``countDocuments()`` takes longer to return, but provides an
28+
**accurate** count of the number of documents and supports specifying a
29+
filter. Choose the appropriate method for your workload.
30+
31+
To specify which documents you wish to count, ``countDocuments()``
32+
accepts a :manual:`query </tutorial/query-documents>` parameter.
33+
``countDocuments()`` will only count the documents that match the
34+
specified query.
35+
36+
``countDocuments()`` and ``estimatedDocumentCount()`` support optional
37+
settings that affect the method's execution. Refer to the reference
38+
documentation for each method for more information.
39+
40+
Depending on whether you specify a Callback method, ``countDocuments()`` and ``estimatedDocumentCount()`` behave in two
41+
different ways:
42+
43+
- If you do not specify a callback method, ``countDocuments()`` and
44+
``estimatedDocumentCount()`` return a Promise that resolves to a
45+
number.
46+
47+
- If you do specify a callback, ``countDocuments()`` and
48+
``estimatedDocumentCount`` return a :node-api:`countCallback
49+
<Collection.html#~countCallback>` object.
50+
51+
Example
52+
-------
53+
54+
The following example estimates the number of documents in the ``movies``
55+
collection in the ``sample_mflix`` database, and then returns an accurate
56+
count of the number of documents in the ``movies`` collection with
57+
``Canada`` in the ``countries`` field.
58+
59+
.. literalinclude:: /code-snippets/usage-examples/count.js
60+
:language: javascript

0 commit comments

Comments
 (0)