Skip to content

Commit 843a96b

Browse files
committed
DOCSP-30519: count usage ex (#70)
(cherry picked from commit b63847b)
1 parent 8506fc9 commit 843a96b

File tree

6 files changed

+158
-34
lines changed

6 files changed

+158
-34
lines changed

source/fundamentals/crud/read-operations/query.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Literal Values
106106
Literal value query filters allow you to query for data that exactly matches
107107
a value you provide in the query filter. The following operation uses a
108108
literal query to search for documents containing a field called ``name``
109-
that has the value of ``pear``:
109+
that has the value of ``"pear"``:
110110

111111
.. io-code-block::
112112
:copyable: true
@@ -356,8 +356,8 @@ Array operators check the values or amount of elements in an array-valued field.
356356
Example
357357
~~~~~~~
358358

359-
The following example matches documents where the ``vendor`` contains
360-
``C``:
359+
The following example matches documents where the ``vendor`` array field
360+
contains ``"C"``:
361361

362362

363363
.. io-code-block::
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::env;
2+
3+
use mongodb::{ bson::doc, Client, Collection };
4+
use serde::{ Deserialize, Serialize };
5+
6+
#[derive(Serialize, Deserialize, Debug)]
7+
struct Restaurant {
8+
name: String,
9+
}
10+
11+
#[tokio::main]
12+
async fn main() -> mongodb::error::Result<()> {
13+
let uri = "<connection string>";
14+
15+
let client = Client::with_uri_str(uri).await?;
16+
let my_coll: Collection<Restaurant> = client
17+
.database("sample_restaurants")
18+
.collection("restaurants");
19+
20+
let ct = my_coll.estimated_document_count(None).await?;
21+
println!("Number of documents: {}", ct);
22+
23+
let ct = my_coll.count_documents(doc! { "name": doc! { "$regex": "Sunset" } }, None).await?;
24+
println!("Number of matching documents: {}", ct);
25+
26+
Ok(())
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::env;
2+
3+
use mongodb::{ bson::doc, sync::{ Client, Collection } };
4+
use serde::{ Deserialize, Serialize };
5+
6+
#[derive(Serialize, Deserialize, Debug)]
7+
struct Restaurant {
8+
name: String,
9+
}
10+
11+
fn main() -> mongodb::error::Result<()> {
12+
let uri = "<connection string>";
13+
14+
let client = Client::with_uri_str(uri)?;
15+
let my_coll: Collection<Restaurant> = client
16+
.database("sample_restaurants")
17+
.collection("restaurants");
18+
19+
let ct = my_coll.estimated_document_count(None)?;
20+
println!("Number of documents: {}", ct);
21+
22+
let ct = my_coll.count_documents(doc! { "name": doc! { "$regex": "Sunset" } }, None)?;
23+
println!("Number of matching documents: {}", ct);
24+
25+
Ok(())
26+
}

source/usage-examples.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Usage Examples
1313
.. toctree::
1414

1515
/usage-examples/find
16+
/usage-examples/count
1617

1718
Overview
1819
--------
@@ -22,11 +23,13 @@ operations. Each usage example includes the following:
2223

2324
- Description of the MongoDB operation
2425
- Asynchronous and synchronous Rust code examples that you can run in
25-
your own environment
26+
your environment
2627
- Output printed by the code example
2728

28-
.. TODO: To learn more about the runtimes offered by the {+driver-short+},
29-
see <add link to runtimes page>.
29+
.. tip:: Asynchronous and Synchronous APIs
30+
31+
To learn more about selecting and using different runtime APIs in the
32+
{+driver-short+}, see the :ref:`rust-runtimes` guide.
3033

3134
How to Use the Usage Examples
3235
-----------------------------
@@ -85,6 +88,7 @@ Available Usage Examples
8588
------------------------
8689

8790
- :ref:`Find Multiple Documents <rust-find-usage>`
91+
- :ref:`Count Documents <rust-count-usage>`
8892

8993
.. TODO: add Usage Example pages as they are created
9094
- :ref:`Find a Document <rust-find-one-usage>`
@@ -95,7 +99,6 @@ Available Usage Examples
9599
- :ref:`Replace a Document <rust-replace-usage>`
96100
- :ref:`Delete a Document <rust-delete-one-usage>`
97101
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
98-
- :ref:`Count Documents <rust-count-usage>`
99102
- :ref:`Find Distinct Values <rust-distinct-usage>`
100103

101104

source/usage-examples/count.txt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. _rust-count-usage:
2+
3+
===============
4+
Count Documents
5+
===============
6+
7+
You can count the number of documents in a collection by calling one of
8+
the following methods on a ``Collection`` instance:
9+
10+
- `count_documents() <{+api+}/struct.Collection.html#method.count_documents>`__:
11+
counts the number of documents that match a query filter. To learn
12+
more about creating query filters, see the :ref:`rust-query-guide`
13+
guide.
14+
15+
- `estimated_document_count() <{+api+}/struct.Collection.html#method.estimated_document_count>`__:
16+
estimates the total number of documents in a collection by using
17+
collection metadata.
18+
19+
Each method returns the count as a ``u64`` instance.
20+
21+
.. note::
22+
23+
If you don't pass a filter to the ``count_documents()`` method,
24+
MongoDB counts the total number of documents in the collection.
25+
26+
Example
27+
-------
28+
29+
This example counts documents in the ``restaurants`` collection of the
30+
``sample_restaurants`` database. The example uses a ``Restaurant``
31+
struct that has a ``name`` field to model the documents in the
32+
collection.
33+
34+
The following code first uses the ``estimated_document_count()`` method
35+
to count the total number of documents in the collection. Then, the
36+
example uses the ``count_documents()`` method to count the number of
37+
documents that match a query filter. The filter matches documents in which
38+
the value of the ``name`` field includes the string ``"Sunset"``.
39+
40+
.. tabs::
41+
42+
.. tab:: Asynchronous
43+
:tabid: find-async
44+
45+
.. io-code-block::
46+
:copyable: true
47+
48+
.. input:: /includes/usage-examples/code-snippets/count-async.rs
49+
:language: rust
50+
:dedent:
51+
52+
.. output::
53+
:language: console
54+
:visible: false
55+
56+
// Your values might differ
57+
Number of documents: 25216
58+
Number of matching documents: 10
59+
60+
.. tab:: Synchronous
61+
:tabid: find-sync
62+
63+
.. io-code-block::
64+
:copyable: true
65+
66+
.. input:: /includes/usage-examples/code-snippets/count-sync.rs
67+
:language: rust
68+
:dedent:
69+
70+
.. output::
71+
:language: console
72+
:visible: false
73+
74+
// Your values might differ
75+
Number of documents: 25216
76+
Number of matching documents: 10

source/usage-examples/find.txt

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,35 @@
44
Find Multiple Documents
55
=======================
66

7-
You can query for multiple documents in a collection by calling the ``find()``
8-
method on a ``Collection`` instance.
7+
You can query for multiple documents in a collection by calling the
8+
`find() <{+api+}/struct.Collection.html#method.find>`__ method on a
9+
``Collection`` instance.
910

1011
Pass a query filter to the ``find()`` method to return documents in the collection
1112
that match the filter. If you do not include a filter, MongoDB returns all the
1213
documents in the collection.
1314

14-
The ``find()`` method returns a ``Cursor`` type, which you can iterate through
15-
to retrieve individual documents.
15+
.. tip::
16+
17+
To learn more about retrieving documents,
18+
see the :ref:`rust-retrieve-guide` guide, and to learn more about
19+
creating query filters, see the :ref:`rust-query-guide` guide.
20+
21+
The ``find()`` method returns a `Cursor <{+api+}/struct.Cursor.html>`__
22+
type, which you can iterate through to retrieve individual documents. To
23+
learn more about using cursors, see the :ref:`rust-cursor-guide` guide.
1624

1725
Example
1826
-------
1927

20-
This example finds documents from the ``sample_restaurants.restaurants`` collection
21-
that match a query filter. The example uses a ``Restaurant`` struct that has ``name``
22-
and ``cuisine`` fields to model the documents in the collection.
28+
This example finds documents that match a query filter in the
29+
``restaurants`` collection of the ``sample_restaurants`` database. The
30+
example uses a ``Restaurant`` struct that has ``name`` and ``cuisine``
31+
fields to model the documents in the collection.
2332

24-
The following code passes a query filter as a parameter to the ``find()`` method. The
25-
filter matches documents where the value of the ``cuisine`` field is ``"French"``.
33+
The following code passes a query filter as a parameter to the
34+
``find()`` method. The filter matches documents in which the value of
35+
the ``cuisine`` field is ``"French"``.
2636

2737
.. tabs::
2838

@@ -65,21 +75,3 @@ filter matches documents where the value of the ``cuisine`` field is ``"French"`
6575
Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" }
6676
Restaurant { name: "Calliope", cuisine: "French" }
6777
...
68-
69-
Additional Information
70-
----------------------
71-
72-
To learn more about retrieving documents, see the :ref:`rust-retrieve-guide` guide.
73-
74-
To learn more about using cursors, see the :ref:`rust-cursor-guide` guide.
75-
76-
To learn more about using query filters, see the :ref:`rust-query-guide` guide.
77-
78-
API Documentation
79-
~~~~~~~~~~~~~~~~~
80-
81-
To learn more about the methods and types mentioned on this
82-
page, see the following API documentation:
83-
84-
- `find() <{+api+}/struct.Collection.html#method.find>`__
85-
- `Cursor <{+api+}/struct.Cursor.html>`__

0 commit comments

Comments
 (0)