diff --git a/source/includes/usage-examples/code-snippets/count-async.rs b/source/includes/usage-examples/code-snippets/count-async.rs index 5606cf19..2740bf2d 100644 --- a/source/includes/usage-examples/code-snippets/count-async.rs +++ b/source/includes/usage-examples/code-snippets/count-async.rs @@ -1,13 +1,24 @@ use std::env; -use mongodb::{ bson::doc, Client, Collection }; -use bson::Document; +use mongodb::{ + bson::{ doc, Document }, + Client, + Collection +}; +use serde::{ Deserialize, Serialize }; + +#[derive(Serialize, Deserialize, Debug)] +struct Restaurant { + name: String, + cuisine: String, +} #[tokio::main] async fn main() -> mongodb::error::Result<()> { let uri = ""; - let client = Client::with_uri_str(uri).await?; - let my_coll: Collection = client + + // Replace with the or type parameter + let my_coll: Collection = client .database("sample_restaurants") .collection("restaurants"); diff --git a/source/includes/usage-examples/code-snippets/count-sync.rs b/source/includes/usage-examples/code-snippets/count-sync.rs index b1e4e08d..051f6a75 100644 --- a/source/includes/usage-examples/code-snippets/count-sync.rs +++ b/source/includes/usage-examples/code-snippets/count-sync.rs @@ -3,12 +3,20 @@ use mongodb::{ bson::{ Document, doc }, sync::{ Client, Collection } }; +use serde::{ Deserialize, Serialize }; + +#[derive(Serialize, Deserialize, Debug)] +struct Restaurant { + name: String, + cuisine: String, +} fn main() -> mongodb::error::Result<()> { let uri = ""; - let client = Client::with_uri_str(uri)?; - let my_coll: Collection = client + + // Replace with the or type parameter + let my_coll: Collection = client .database("sample_restaurants") .collection("restaurants"); diff --git a/source/includes/usage-examples/code-snippets/distinct-async.rs b/source/includes/usage-examples/code-snippets/distinct-async.rs index f753e8a7..74a00999 100644 --- a/source/includes/usage-examples/code-snippets/distinct-async.rs +++ b/source/includes/usage-examples/code-snippets/distinct-async.rs @@ -3,13 +3,22 @@ use mongodb::{ bson::{ Document, doc }, Client, Collection }; +use serde::{ Deserialize, Serialize }; + +#[derive(Serialize, Deserialize, Debug)] +struct Restaurant { + name: String, + cuisine: String, + borough: String, +} #[tokio::main] async fn main() -> mongodb::error::Result<()> { let uri = ""; - let client = Client::with_uri_str(uri).await?; - let my_coll: Collection = client + + // Replace with the or type parameter + let my_coll: Collection = client .database("sample_restaurants") .collection("restaurants"); diff --git a/source/includes/usage-examples/code-snippets/distinct-sync.rs b/source/includes/usage-examples/code-snippets/distinct-sync.rs index 16f77f7a..7b68ac93 100644 --- a/source/includes/usage-examples/code-snippets/distinct-sync.rs +++ b/source/includes/usage-examples/code-snippets/distinct-sync.rs @@ -3,12 +3,21 @@ use mongodb::{ bson::{ Document, doc }, sync::{ Client, Collection } }; +use serde::{ Deserialize, Serialize }; + +#[derive(Serialize, Deserialize, Debug)] +struct Restaurant { + name: String, + cuisine: String, + borough: String, +} fn main() -> mongodb::error::Result<()> { let uri = ""; - let client = Client::with_uri_str(uri)?; - let my_coll: Collection = client + + // Replace with the or type parameter + let my_coll: Collection = client .database("sample_restaurants") .collection("restaurants"); diff --git a/source/usage-examples/count.txt b/source/usage-examples/count.txt index f6423e3c..977319f4 100644 --- a/source/usage-examples/count.txt +++ b/source/usage-examples/count.txt @@ -27,13 +27,19 @@ Example ------- This example counts documents in the ``restaurants`` collection of the -``sample_restaurants`` database. - -The following code first uses the ``estimated_document_count()`` method +``sample_restaurants`` database. The example uses the ``estimated_document_count()`` method to count the total number of documents in the collection. Then, the example uses the ``count_documents()`` method to count the number of -documents that match a query filter. The filter matches documents in which -the value of the ``name`` field includes the string ``"Sunset"``: +documents in which the value of the ``name`` field includes the string ``"Sunset"``. + +You can access the documents in the ``restaurants`` collection as instances +of the ``Document`` type or a custom data type. To specify which data type represents +the collection's data, replace the ```` type parameter on the highlighted +line with one of the following values: + +- ````: Represents collection documents as BSON documents +- ````: Represents collection documents as instances of the ``Restaurant`` + struct, defined at the top of the code Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to see the corresponding code for each runtime: @@ -48,6 +54,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/count-async.rs :language: rust + :emphasize-lines: 21 :dedent: .. output:: @@ -66,6 +73,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/count-sync.rs :language: rust + :emphasize-lines: 19 :dedent: .. output:: diff --git a/source/usage-examples/distinct.txt b/source/usage-examples/distinct.txt index aca89be8..d25b16a1 100644 --- a/source/usage-examples/distinct.txt +++ b/source/usage-examples/distinct.txt @@ -31,11 +31,19 @@ Example This example finds distinct values for a field in the ``restaurants`` collection of the ``sample_restaurants`` database. - -This example finds distinct values of the ``borough`` field in +The ``distinct()`` method retrieves distinct values of the ``borough`` field in the subset of documents in which the value of the ``cuisine`` field is ``"Turkish"``. +You can access the documents in the ``restaurants`` collection as instances +of the ``Document`` type or a custom data type. To specify which data type represents +the collection's data, replace the ```` type parameter on the highlighted +line with one of the following values: + +- ````: Represents collection documents as BSON documents +- ````: Represents collection documents as instances of the ``Restaurant`` + struct, defined at the top of the code + Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to see the corresponding code for each runtime: @@ -49,6 +57,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/distinct-async.rs :language: rust + :emphasize-lines: 21 :dedent: .. output:: @@ -69,6 +78,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/distinct-sync.rs :language: rust + :emphasize-lines: 20 :dedent: .. output::