From 6aed6312d11e8643189f530514735201039b1a7f Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 4 Dec 2024 10:44:01 -0500 Subject: [PATCH 1/4] DOCSP-44969: Modify insert usage examples --- .../code-snippets/insert-one-async.rs | 14 +++++++++--- .../code-snippets/insert-one-sync.rs | 14 +++++++++--- source/usage-examples/insertOne.txt | 22 ++++++++++++++----- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/insert-one-async.rs b/source/includes/usage-examples/code-snippets/insert-one-async.rs index 6d672452..b04df7c8 100644 --- a/source/includes/usage-examples/code-snippets/insert-one-async.rs +++ b/source/includes/usage-examples/code-snippets/insert-one-async.rs @@ -14,17 +14,25 @@ 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"); - let doc = Restaurant { + let insert_doc = doc! { + "name": "Sea Stone Tavern", + "cuisine": "Greek", + "borough": "Queens", + }; + let insert_struct = Restaurant { name: "Sea Stone Tavern".to_string(), cuisine: "Greek".to_string(), borough: "Queens".to_string(), }; - let res = my_coll.insert_one(doc).await?; + // Replace with the insert_struct or insert_doc variable + let res = my_coll.insert_one().await?; println!("Inserted a document with _id: {}", res.inserted_id); Ok(()) diff --git a/source/includes/usage-examples/code-snippets/insert-one-sync.rs b/source/includes/usage-examples/code-snippets/insert-one-sync.rs index 7983c3f7..eb5daa66 100644 --- a/source/includes/usage-examples/code-snippets/insert-one-sync.rs +++ b/source/includes/usage-examples/code-snippets/insert-one-sync.rs @@ -13,17 +13,25 @@ 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"); - let doc = Restaurant { + let insert_doc = doc! { + "name": "Sea Stone Tavern", + "cuisine": "Greek", + "borough": "Queens", + }; + let insert_struct = Restaurant { name: "Sea Stone Tavern".to_string(), cuisine: "Greek".to_string(), borough: "Queens".to_string(), }; - let res = my_coll.insert_one(doc).run()?; + // Replace with the insert_struct or insert_doc variable + let res = my_coll.insert_one().run()?; println!("Inserted a document with _id: {}", res.inserted_id); Ok(()) diff --git a/source/usage-examples/insertOne.txt b/source/usage-examples/insertOne.txt index a0e20e21..351b571e 100644 --- a/source/usage-examples/insertOne.txt +++ b/source/usage-examples/insertOne.txt @@ -27,12 +27,22 @@ Example ------- This example inserts a document into the ``restaurants`` collection of -the ``sample_restaurants`` database. The example uses a ``Restaurant`` -struct that has ``name``, ``borough``, and ``cuisine`` fields to model -documents in the collection. +the ``sample_restaurants`` database. The ``insert_one()`` method +inserts a document that has ``name``, ``borough``, and ``cuisine`` field +values. -The following code creates a ``Restaurant`` instance and inserts it into -the collection. +You can insert this document as an instance of the ``Document`` type or a +custom data type. To specify which data type represents the collection's +data, perform the following actions on the highlighted lines: + +- To access and insert collection documents as BSON documents, replace the ```` type + parameter with ```` and the ```` placeholder with + ``insert_doc``. + +- To access and insert collection documents as instances of the ``Restaurant`` struct, + replace the ```` type parameter with ```` and the ```` + placeholder with ``insert_struct``. The ``Restaurant`` struct is defined at + the top of the code file. Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to see the corresponding code for each runtime: @@ -47,6 +57,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/insert-one-async.rs :language: rust + :emphasize-lines: 19, 35 :dedent: .. output:: @@ -63,6 +74,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/insert-one-sync.rs :language: rust + :emphasize-lines: 18, 34 :dedent: .. output:: From a8e14875398678f5ab9befde5c093cf721a4d294 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 4 Dec 2024 11:00:40 -0500 Subject: [PATCH 2/4] insert many changes --- .../code-snippets/insert-many-async.rs | 18 +++++++++++--- .../code-snippets/insert-many-sync.rs | 18 +++++++++++--- source/usage-examples/insertMany.txt | 24 ++++++++++++++----- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/insert-many-async.rs b/source/includes/usage-examples/code-snippets/insert-many-async.rs index 8fdf5771..487bc893 100644 --- a/source/includes/usage-examples/code-snippets/insert-many-async.rs +++ b/source/includes/usage-examples/code-snippets/insert-many-async.rs @@ -16,11 +16,22 @@ 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"); - let docs = vec! [ + let insert_docs = vec! [ + doc! { + "name": "While in Kathmandu", + "cuisine": "Nepalese", + }; + doc! { + "name": "Cafe Himalaya", + "cuisine": "Nepalese", + }; + ]; + let insert_structs = vec! [ Restaurant { name: "While in Kathmandu".to_string(), cuisine: "Nepalese".to_string(), @@ -31,7 +42,8 @@ async fn main() -> mongodb::error::Result<()> { } ]; - let insert_many_result = my_coll.insert_many(docs).await?; + // Replace with the insert_structs or insert_docs variable + let insert_many_result = my_coll.insert_many().await?; println!("Inserted documents with _ids:"); for (_key, value) in &insert_many_result.inserted_ids { println!("{}", value); diff --git a/source/includes/usage-examples/code-snippets/insert-many-sync.rs b/source/includes/usage-examples/code-snippets/insert-many-sync.rs index cbfc7579..3ee90479 100644 --- a/source/includes/usage-examples/code-snippets/insert-many-sync.rs +++ b/source/includes/usage-examples/code-snippets/insert-many-sync.rs @@ -14,11 +14,22 @@ 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"); - let docs = vec! [ + let insert_docs = vec! [ + doc! { + "name": "While in Kathmandu", + "cuisine": "Nepalese", + }; + doc! { + "name": "Cafe Himalaya", + "cuisine": "Nepalese", + }; + ]; + let insert_structs = vec! [ Restaurant { name: "While in Kathmandu".to_string(), cuisine: "Nepalese".to_string(), @@ -29,7 +40,8 @@ fn main() -> mongodb::error::Result<()> { } ]; - let insert_many_result = my_coll.insert_many(docs).run()?; + // Replace with the insert_structs or insert_docs variable + let insert_many_result = my_coll.insert_many().run()?; println!("Inserted documents with _ids:"); for (_key, value) in &insert_many_result.inserted_ids { println!("{}", value); diff --git a/source/usage-examples/insertMany.txt b/source/usage-examples/insertMany.txt index f918dd37..3bb98767 100644 --- a/source/usage-examples/insertMany.txt +++ b/source/usage-examples/insertMany.txt @@ -30,13 +30,23 @@ To learn more about inserting documents into a collection, see the Example ------- -This example inserts documents into the ``restaurants`` collection of the -``sample_restaurants`` database. The example uses a ``Restaurant`` struct containing -``name`` and ``cuisine`` fields to model the documents being inserted into the -collection. +This example inserts multiple documents into the ``restaurants`` collection of the +``sample_restaurants`` database. The example inserts documents that have +``name`` and ``cuisine`` field values by passing a vector of documents +to the ``insert_many()`` method. -This example passes a vector of documents as a parameter to the ``insert_many()`` -method. +You can insert these documents as instances of the ``Document`` type or a +custom data type. To specify which data type represents the collection's +data, perform the following actions on the highlighted lines: + +- To access and insert collection documents as BSON documents, replace the ```` type + parameter with ```` and the ```` placeholder with + ``insert_docs``. + +- To access and insert collection documents as instances of the ``Restaurant`` struct, + replace the ```` type parameter with ```` and the ```` + placeholder with ``insert_structs``. The ``Restaurant`` struct is defined at + the top of the code file. Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to see the corresponding code for each runtime: @@ -51,6 +61,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/insert-many-async.rs :language: rust + :emphasize-lines: 20, 46 :dedent: .. output:: @@ -69,6 +80,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/insert-many-sync.rs :language: rust + :emphasize-lines: 18, 44 :dedent: .. output:: From c6a96cbc8fb5edf4a878d55ae8604af295ef1268 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 4 Dec 2024 11:22:57 -0500 Subject: [PATCH 3/4] code edits --- .../usage-examples/code-snippets/insert-many-async.rs | 6 +++--- .../usage-examples/code-snippets/insert-many-sync.rs | 6 +++--- .../usage-examples/code-snippets/insert-one-async.rs | 6 +++++- .../usage-examples/code-snippets/insert-one-sync.rs | 5 ++++- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/insert-many-async.rs b/source/includes/usage-examples/code-snippets/insert-many-async.rs index 487bc893..944ee1ef 100644 --- a/source/includes/usage-examples/code-snippets/insert-many-async.rs +++ b/source/includes/usage-examples/code-snippets/insert-many-async.rs @@ -1,5 +1,5 @@ use mongodb::{ - bson::doc, + bson::{doc, Document}, Client, Collection }; @@ -25,11 +25,11 @@ async fn main() -> mongodb::error::Result<()> { doc! { "name": "While in Kathmandu", "cuisine": "Nepalese", - }; + }, doc! { "name": "Cafe Himalaya", "cuisine": "Nepalese", - }; + } ]; let insert_structs = vec! [ Restaurant { diff --git a/source/includes/usage-examples/code-snippets/insert-many-sync.rs b/source/includes/usage-examples/code-snippets/insert-many-sync.rs index 3ee90479..69795de4 100644 --- a/source/includes/usage-examples/code-snippets/insert-many-sync.rs +++ b/source/includes/usage-examples/code-snippets/insert-many-sync.rs @@ -1,5 +1,5 @@ use mongodb::{ - bson::doc, + bson::{doc, Document}, sync::{Client, Collection} }; use serde::{ Deserialize, Serialize }; @@ -23,11 +23,11 @@ fn main() -> mongodb::error::Result<()> { doc! { "name": "While in Kathmandu", "cuisine": "Nepalese", - }; + }, doc! { "name": "Cafe Himalaya", "cuisine": "Nepalese", - }; + } ]; let insert_structs = vec! [ Restaurant { diff --git a/source/includes/usage-examples/code-snippets/insert-one-async.rs b/source/includes/usage-examples/code-snippets/insert-one-async.rs index b04df7c8..f8401d60 100644 --- a/source/includes/usage-examples/code-snippets/insert-one-async.rs +++ b/source/includes/usage-examples/code-snippets/insert-one-async.rs @@ -1,5 +1,9 @@ use std::env; -use mongodb::{ bson::doc, Client, Collection }; +use mongodb::{ + bson::{doc, Document}, + Client, + Collection +}; use serde::{ Deserialize, Serialize }; #[derive(Serialize, Deserialize, Debug)] diff --git a/source/includes/usage-examples/code-snippets/insert-one-sync.rs b/source/includes/usage-examples/code-snippets/insert-one-sync.rs index eb5daa66..062f01fd 100644 --- a/source/includes/usage-examples/code-snippets/insert-one-sync.rs +++ b/source/includes/usage-examples/code-snippets/insert-one-sync.rs @@ -1,5 +1,8 @@ use std::env; -use mongodb::{ bson::doc, sync::{ Client, Collection } }; +use mongodb::{ + bson::{doc, Document}, + sync::{ Client, Collection } +}; use serde::{ Deserialize, Serialize }; #[derive(Serialize, Deserialize, Debug)] From 3e755de2a0ff3686c1977df39de6baf9b539d251 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 4 Dec 2024 13:22:14 -0500 Subject: [PATCH 4/4] SA feedback --- source/usage-examples/insertOne.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/usage-examples/insertOne.txt b/source/usage-examples/insertOne.txt index 351b571e..3cd242bc 100644 --- a/source/usage-examples/insertOne.txt +++ b/source/usage-examples/insertOne.txt @@ -57,7 +57,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/insert-one-async.rs :language: rust - :emphasize-lines: 19, 35 + :emphasize-lines: 23, 39 :dedent: .. output:: @@ -74,7 +74,7 @@ see the corresponding code for each runtime: .. input:: /includes/usage-examples/code-snippets/insert-one-sync.rs :language: rust - :emphasize-lines: 18, 34 + :emphasize-lines: 21, 37 :dedent: .. output::