From a84a5d172d7a0e1f4aa51ac28d89e4ff636324bc Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 6 Feb 2025 11:14:54 -0500 Subject: [PATCH 1/5] DOCSP-44971: Count & distinct usage ex updates --- .../code-snippets/count-async.rs | 12 ++++++++++-- .../usage-examples/code-snippets/count-sync.rs | 12 ++++++++++-- source/usage-examples/count.txt | 18 +++++++++++++----- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/count-async.rs b/source/includes/usage-examples/code-snippets/count-async.rs index 5606cf19..b8ba8181 100644 --- a/source/includes/usage-examples/code-snippets/count-async.rs +++ b/source/includes/usage-examples/code-snippets/count-async.rs @@ -1,13 +1,21 @@ use std::env; use mongodb::{ bson::doc, Client, Collection }; use bson::Document; +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/usage-examples/count.txt b/source/usage-examples/count.txt index f6423e3c..5d897e80 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: + +- ````: Accesses collection documents as BSON documents +- ````: Accesses 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: 17 :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: 18 :dedent: .. output:: From bd39a97bf05b21d7af4c577a876cb39e4dac52bf Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 6 Feb 2025 11:28:55 -0500 Subject: [PATCH 2/5] distinct --- .../usage-examples/code-snippets/distinct-async.rs | 13 +++++++++++-- .../usage-examples/code-snippets/distinct-sync.rs | 13 +++++++++++-- source/usage-examples/count.txt | 4 ++-- source/usage-examples/distinct.txt | 14 ++++++++++++-- 4 files changed, 36 insertions(+), 8 deletions(-) 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 5d897e80..dc4f2a18 100644 --- a/source/usage-examples/count.txt +++ b/source/usage-examples/count.txt @@ -54,7 +54,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/count-async.rs :language: rust - :emphasize-lines: 17 + :emphasize-lines: 18 :dedent: .. output:: @@ -73,7 +73,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/count-sync.rs :language: rust - :emphasize-lines: 18 + :emphasize-lines: 19 :dedent: .. output:: diff --git a/source/usage-examples/distinct.txt b/source/usage-examples/distinct.txt index aca89be8..1f5b4a9e 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: + +- ````: Accesses collection documents as BSON documents +- ````: Accesses 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:: From ee6b8ed000ffae847467361716df639d573fef5a Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 6 Feb 2025 11:42:57 -0500 Subject: [PATCH 3/5] edits --- .../includes/usage-examples/code-snippets/count-async.rs | 7 +++++-- source/usage-examples/count.txt | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/count-async.rs b/source/includes/usage-examples/code-snippets/count-async.rs index b8ba8181..2c24063e 100644 --- a/source/includes/usage-examples/code-snippets/count-async.rs +++ b/source/includes/usage-examples/code-snippets/count-async.rs @@ -1,6 +1,9 @@ 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)] diff --git a/source/usage-examples/count.txt b/source/usage-examples/count.txt index dc4f2a18..b22c56ca 100644 --- a/source/usage-examples/count.txt +++ b/source/usage-examples/count.txt @@ -54,7 +54,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/count-async.rs :language: rust - :emphasize-lines: 18 + :emphasize-lines: 21 :dedent: .. output:: From 7d2a1550622d6ab4803264c8c8de030dd7a91d23 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 6 Feb 2025 11:44:36 -0500 Subject: [PATCH 4/5] spacing --- source/includes/usage-examples/code-snippets/count-async.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/includes/usage-examples/code-snippets/count-async.rs b/source/includes/usage-examples/code-snippets/count-async.rs index 2c24063e..2740bf2d 100644 --- a/source/includes/usage-examples/code-snippets/count-async.rs +++ b/source/includes/usage-examples/code-snippets/count-async.rs @@ -1,6 +1,6 @@ use std::env; use mongodb::{ - bson::{ doc, Document}, + bson::{ doc, Document }, Client, Collection }; From 9e071d1120ff72212d61512322575f59fbdac425 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 6 Feb 2025 15:40:44 -0500 Subject: [PATCH 5/5] MW feedback --- source/usage-examples/count.txt | 4 ++-- source/usage-examples/distinct.txt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/usage-examples/count.txt b/source/usage-examples/count.txt index b22c56ca..977319f4 100644 --- a/source/usage-examples/count.txt +++ b/source/usage-examples/count.txt @@ -37,8 +37,8 @@ of the ``Document`` type or a custom data type. To specify which data type repre the collection's data, replace the ```` type parameter on the highlighted line with one of the following values: -- ````: Accesses collection documents as BSON documents -- ````: Accesses collection documents as instances of the ``Restaurant`` +- ````: 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 diff --git a/source/usage-examples/distinct.txt b/source/usage-examples/distinct.txt index 1f5b4a9e..d25b16a1 100644 --- a/source/usage-examples/distinct.txt +++ b/source/usage-examples/distinct.txt @@ -40,8 +40,8 @@ of the ``Document`` type or a custom data type. To specify which data type repre the collection's data, replace the ```` type parameter on the highlighted line with one of the following values: -- ````: Accesses collection documents as BSON documents -- ````: Accesses collection documents as instances of the ``Restaurant`` +- ````: 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