Skip to content

Commit 0838cc2

Browse files
authored
DOCSP-30520: distinct usage example (#72)
1 parent 0967895 commit 0838cc2

File tree

6 files changed

+138
-3
lines changed

6 files changed

+138
-3
lines changed

snooty.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ docs-branch = "master" # always set this to the docs branch (i.e. master, v2.6,
2222
version = "2.7.1" # always set this to the driver version (i.e. 2.6.0, 2.5.0, etc.)
2323
min-rust-version = "1.60" # always set this to the minimum supported Rust version
2424
api = "https://docs.rs/mongodb/{+version+}/mongodb"
25+
bson-version = "2.7.0"
26+
bson-api = "https://docs.rs/bson/{+bson-version+}/bson"
2527
stable-api = "Stable API"
2628
tracing-version = "0.1.37"
2729
tracing-sub-version = "0.3.17"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
borough: String,
9+
cuisine: String,
10+
}
11+
12+
#[tokio::main]
13+
async fn main() -> mongodb::error::Result<()> {
14+
let uri = "<connection string>";
15+
16+
let client = Client::with_uri_str(uri).await?;
17+
let my_coll: Collection<Restaurant> = client
18+
.database("sample_restaurants")
19+
.collection("restaurants");
20+
21+
let filter = doc! { "cuisine": "Turkish" };
22+
let boroughs = my_coll.distinct("borough", filter, None).await?;
23+
24+
println!("List of field values for 'borough':");
25+
for b in boroughs.iter() {
26+
println!("{:?}", b);
27+
}
28+
29+
Ok(())
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
borough: String,
9+
cuisine: String,
10+
}
11+
12+
fn main() -> mongodb::error::Result<()> {
13+
let uri = "<connection string>";
14+
15+
let client = Client::with_uri_str(uri)?;
16+
let my_coll: Collection<Restaurant> = client
17+
.database("sample_restaurants")
18+
.collection("restaurants");
19+
20+
let filter = doc! { "cuisine": "Turkish" };
21+
let boroughs = my_coll.distinct("borough", filter, None)?;
22+
23+
println!("List of field values for 'borough':");
24+
for b in boroughs.iter() {
25+
println!("{:?}", b);
26+
}
27+
28+
Ok(())
29+
}

source/usage-examples.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Usage Examples
1919
/usage-examples/replace
2020
/usage-examples/deleteMany
2121
/usage-examples/count
22+
/usage-examples/distinct
2223

2324
Overview
2425
--------
@@ -99,11 +100,12 @@ Available Usage Examples
99100
- :ref:`Replace a Document <rust-replace-usage>`
100101
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
101102
- :ref:`Count Documents <rust-count-usage>`
102-
103+
- :ref:`List Distinct Field Values <rust-distinct-usage>`
104+
103105
.. TODO: add Usage Example pages as they are created
104106
- :ref:`Insert a Document <rust-insert-one-usage>`
105107
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
106108
- :ref:`Update a Document <rust-update-one-usage>`
107109
- :ref:`Update Multiple Documents <rust-update-many-usage>`
108110
- :ref:`Delete a Document <rust-delete-one-usage>`
109-
- :ref:`Find Distinct Values <rust-distinct-usage>`
111+
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`

source/usage-examples/count.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The following code first uses the ``estimated_document_count()`` method
3535
to count the total number of documents in the collection. Then, the
3636
example uses the ``count_documents()`` method to count the number of
3737
documents that match a query filter. The filter matches documents in which
38-
the value of the ``name`` field includes the string ``"Sunset"``.
38+
the value of the ``name`` field includes the string ``"Sunset"``:
3939

4040
Select the **Asynchronous** or **Synchronous** tab to see the
4141
corresponding code for each runtime:

source/usage-examples/distinct.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
.. _rust-distinct-usage:
2+
3+
==========================
4+
List Distinct Field Values
5+
==========================
6+
7+
You can list the distinct field values of documents by calling the
8+
`distinct() <{+api+}/struct.Collection.html#method.distinct>`__ method
9+
on a ``Collection`` instance.
10+
11+
Pass a field name as a parameter to the ``distinct()`` method to return
12+
the distinct values for that field. You can also pass a query filter as
13+
a parameter to find distinct field values from only a subset of matched
14+
documents. To learn more about creating query filters, see the
15+
:ref:`rust-query-guide` guide.
16+
17+
The ``distinct()`` method returns the list of distinct values as a ``Vec<Bson>``
18+
type, a vector of `Bson <{+bson-api+}/enum.Bson.html>`__ values.
19+
20+
Example
21+
-------
22+
23+
This example finds distinct field values across the
24+
``restaurants`` collection of the ``sample_restaurants`` database. The
25+
example uses a ``Restaurant`` struct that has ``cuisine`` and
26+
``borough`` fields to model documents in the collection.
27+
28+
The following code finds distinct values of the ``borough`` field in
29+
the subset of documents in which the value of the ``cuisine`` field
30+
is ``"Turkish"``:
31+
32+
.. tabs::
33+
34+
.. tab:: Asynchronous
35+
:tabid: find-async
36+
37+
.. io-code-block::
38+
:copyable: true
39+
40+
.. input:: /includes/usage-examples/code-snippets/distinct-async.rs
41+
:language: rust
42+
:dedent:
43+
44+
.. output::
45+
:language: console
46+
:visible: false
47+
48+
List of field values for 'borough':
49+
String("Brooklyn")
50+
String("Manhattan")
51+
String("Queens")
52+
String("Staten Island")
53+
54+
.. tab:: Synchronous
55+
:tabid: find-sync
56+
57+
.. io-code-block::
58+
:copyable: true
59+
60+
.. input:: /includes/usage-examples/code-snippets/distinct-sync.rs
61+
:language: rust
62+
:dedent:
63+
64+
.. output::
65+
:language: console
66+
:visible: false
67+
68+
List of field values for 'borough':
69+
String("Brooklyn")
70+
String("Manhattan")
71+
String("Queens")
72+
String("Staten Island")

0 commit comments

Comments
 (0)