Skip to content

Commit 0b34778

Browse files
committed
DOCSP-44971: Count & distinct usage ex updates (#163)
(cherry picked from commit c2614e6)
1 parent eb4a292 commit 0b34778

File tree

6 files changed

+72
-17
lines changed

6 files changed

+72
-17
lines changed

source/includes/usage-examples/code-snippets/count-async.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
use std::env;
2-
use mongodb::{ bson::doc, Client, Collection };
3-
use bson::Document;
2+
use mongodb::{
3+
bson::{ doc, Document },
4+
Client,
5+
Collection
6+
};
7+
use serde::{ Deserialize, Serialize };
8+
9+
#[derive(Serialize, Deserialize, Debug)]
10+
struct Restaurant {
11+
name: String,
12+
cuisine: String,
13+
}
414

515
#[tokio::main]
616
async fn main() -> mongodb::error::Result<()> {
717
let uri = "<connection string>";
8-
918
let client = Client::with_uri_str(uri).await?;
10-
let my_coll: Collection<Document> = client
19+
20+
// Replace <T> with the <Document> or <Restaurant> type parameter
21+
let my_coll: Collection<T> = client
1122
.database("sample_restaurants")
1223
.collection("restaurants");
1324

source/includes/usage-examples/code-snippets/count-sync.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ use mongodb::{
33
bson::{ Document, doc },
44
sync::{ Client, Collection }
55
};
6+
use serde::{ Deserialize, Serialize };
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
struct Restaurant {
10+
name: String,
11+
cuisine: String,
12+
}
613

714
fn main() -> mongodb::error::Result<()> {
815
let uri = "<connection string>";
9-
1016
let client = Client::with_uri_str(uri)?;
11-
let my_coll: Collection<Document> = client
17+
18+
// Replace <T> with the <Document> or <Restaurant> type parameter
19+
let my_coll: Collection<T> = client
1220
.database("sample_restaurants")
1321
.collection("restaurants");
1422

source/includes/usage-examples/code-snippets/distinct-async.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@ use mongodb::{
33
bson::{ Document, doc },
44
Client,
55
Collection };
6+
use serde::{ Deserialize, Serialize };
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
struct Restaurant {
10+
name: String,
11+
cuisine: String,
12+
borough: String,
13+
}
614

715
#[tokio::main]
816
async fn main() -> mongodb::error::Result<()> {
917
let uri = "<connection string>";
10-
1118
let client = Client::with_uri_str(uri).await?;
12-
let my_coll: Collection<Document> = client
19+
20+
// Replace <T> with the <Document> or <Restaurant> type parameter
21+
let my_coll: Collection<T> = client
1322
.database("sample_restaurants")
1423
.collection("restaurants");
1524

source/includes/usage-examples/code-snippets/distinct-sync.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ use mongodb::{
33
bson::{ Document, doc },
44
sync::{ Client, Collection }
55
};
6+
use serde::{ Deserialize, Serialize };
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
struct Restaurant {
10+
name: String,
11+
cuisine: String,
12+
borough: String,
13+
}
614

715
fn main() -> mongodb::error::Result<()> {
816
let uri = "<connection string>";
9-
1017
let client = Client::with_uri_str(uri)?;
11-
let my_coll: Collection<Document> = client
18+
19+
// Replace <T> with the <Document> or <Restaurant> type parameter
20+
let my_coll: Collection<T> = client
1221
.database("sample_restaurants")
1322
.collection("restaurants");
1423

source/usage-examples/count.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ Example
2727
-------
2828

2929
This example counts documents in the ``restaurants`` collection of the
30-
``sample_restaurants`` database.
31-
32-
The following code first uses the ``estimated_document_count()`` method
30+
``sample_restaurants`` database. The example uses the ``estimated_document_count()`` method
3331
to count the total number of documents in the collection. Then, the
3432
example uses the ``count_documents()`` method to count the number of
35-
documents that match a query filter. The filter matches documents in which
36-
the value of the ``name`` field includes the string ``"Sunset"``:
33+
documents in which the value of the ``name`` field includes the string ``"Sunset"``.
34+
35+
You can access the documents in the ``restaurants`` collection as instances
36+
of the ``Document`` type or a custom data type. To specify which data type represents
37+
the collection's data, replace the ``<T>`` type parameter on the highlighted
38+
line with one of the following values:
39+
40+
- ``<Document>``: Represents collection documents as BSON documents
41+
- ``<Restaurant>``: Represents collection documents as instances of the ``Restaurant``
42+
struct, defined at the top of the code
3743

3844
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
3945
see the corresponding code for each runtime:
@@ -48,6 +54,7 @@ see the corresponding code for each runtime:
4854

4955
.. input:: /includes/usage-examples/code-snippets/count-async.rs
5056
:language: rust
57+
:emphasize-lines: 21
5158
:dedent:
5259

5360
.. output::
@@ -66,6 +73,7 @@ see the corresponding code for each runtime:
6673

6774
.. input:: /includes/usage-examples/code-snippets/count-sync.rs
6875
:language: rust
76+
:emphasize-lines: 19
6977
:dedent:
7078

7179
.. output::

source/usage-examples/distinct.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ Example
3131

3232
This example finds distinct values for a field in the
3333
``restaurants`` collection of the ``sample_restaurants`` database.
34-
35-
This example finds distinct values of the ``borough`` field in
34+
The ``distinct()`` method retrieves distinct values of the ``borough`` field in
3635
the subset of documents in which the value of the ``cuisine`` field
3736
is ``"Turkish"``.
3837

38+
You can access the documents in the ``restaurants`` collection as instances
39+
of the ``Document`` type or a custom data type. To specify which data type represents
40+
the collection's data, replace the ``<T>`` type parameter on the highlighted
41+
line with one of the following values:
42+
43+
- ``<Document>``: Represents collection documents as BSON documents
44+
- ``<Restaurant>``: Represents collection documents as instances of the ``Restaurant``
45+
struct, defined at the top of the code
46+
3947
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
4048
see the corresponding code for each runtime:
4149

@@ -49,6 +57,7 @@ see the corresponding code for each runtime:
4957

5058
.. input:: /includes/usage-examples/code-snippets/distinct-async.rs
5159
:language: rust
60+
:emphasize-lines: 21
5261
:dedent:
5362

5463
.. output::
@@ -69,6 +78,7 @@ see the corresponding code for each runtime:
6978

7079
.. input:: /includes/usage-examples/code-snippets/distinct-sync.rs
7180
:language: rust
81+
:emphasize-lines: 20
7282
:dedent:
7383

7484
.. output::

0 commit comments

Comments
 (0)