diff --git a/source/fundamentals/crud/read-operations/skip.txt b/source/fundamentals/crud/read-operations/skip.txt index 31237a4a..84b8b3d1 100644 --- a/source/fundamentals/crud/read-operations/skip.txt +++ b/source/fundamentals/crud/read-operations/skip.txt @@ -172,6 +172,8 @@ To learn more about the operations mentioned in this guide, see the following gu - :ref:`rust-aggregation` - :ref:`rust-sort-guide` +.. - :ref:`rust-limit-guide` + API Documentation ~~~~~~~~~~~~~~~~~ diff --git a/source/includes/usage-examples/code-snippets/find-async.rs b/source/includes/usage-examples/code-snippets/find-async.rs index 86d46ba7..59f515ca 100644 --- a/source/includes/usage-examples/code-snippets/find-async.rs +++ b/source/includes/usage-examples/code-snippets/find-async.rs @@ -17,7 +17,8 @@ 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"); @@ -26,7 +27,7 @@ async fn main() -> mongodb::error::Result<()> { ).await?; while let Some(doc) = cursor.try_next().await? { - println!("{:?}", doc); + println!("{:#?}", doc); } Ok(()) diff --git a/source/includes/usage-examples/code-snippets/find-one-async.rs b/source/includes/usage-examples/code-snippets/find-one-async.rs index ed2c5c30..5de01b78 100644 --- a/source/includes/usage-examples/code-snippets/find-one-async.rs +++ b/source/includes/usage-examples/code-snippets/find-one-async.rs @@ -16,7 +16,8 @@ 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/find-one-sync.rs b/source/includes/usage-examples/code-snippets/find-one-sync.rs index d138e2af..6517589f 100644 --- a/source/includes/usage-examples/code-snippets/find-one-sync.rs +++ b/source/includes/usage-examples/code-snippets/find-one-sync.rs @@ -14,7 +14,8 @@ 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/find-sync.rs b/source/includes/usage-examples/code-snippets/find-sync.rs index 8dd49f6e..b639187d 100644 --- a/source/includes/usage-examples/code-snippets/find-sync.rs +++ b/source/includes/usage-examples/code-snippets/find-sync.rs @@ -14,7 +14,8 @@ 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"); @@ -23,7 +24,7 @@ fn main() -> mongodb::error::Result<()> { ).run()?; for result in cursor { - println!("{:?}", result?); + println!("{:#?}", result?); } Ok(()) diff --git a/source/usage-examples/find.txt b/source/usage-examples/find.txt index 44eb622e..7bbb5c77 100644 --- a/source/usage-examples/find.txt +++ b/source/usage-examples/find.txt @@ -4,6 +4,19 @@ Find Multiple Documents ======================= +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: read, code example + +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + You can query for multiple documents in a collection by calling the `find() <{+api+}/struct.Collection.html#method.find>`__ method on a ``Collection`` instance. @@ -26,12 +39,16 @@ Example ------- This example retrieves documents that match a query filter from the ``restaurants`` -collection in the ``sample_restaurants`` database. The example populates -instances of the ``Restaurant`` struct with data from the retrieved -documents. +collection in the ``sample_restaurants`` database. The ``find()`` method returns +all documents in which the value of the ``cuisine`` field is ``"French"``. -The following code uses a query filter that matches documents in which the value -of the ``cuisine`` field is ``"French"``. +You can model each retrieved document as a BSON data 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: + +- ````: Retrieves and prints collection documents as BSON documents +- ````: Retrieves and prints 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: @@ -39,41 +56,75 @@ see the corresponding code for each runtime: .. tabs:: .. tab:: Asynchronous - :tabid: find-async - - .. io-code-block:: - :copyable: true + :tabid: find-one-async - .. input:: /includes/usage-examples/code-snippets/find-async.rs - :language: rust - :dedent: - - .. output:: - :language: console - :visible: false - - // Results truncated - ... - Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" } - Restaurant { name: "Calliope", cuisine: "French" } - ... + .. literalinclude:: /includes/usage-examples/code-snippets/find-async.rs + :language: rust + :emphasize-lines: 21 + :dedent: .. tab:: Synchronous - :tabid: find-sync + :tabid: find-one-sync - .. io-code-block:: - :copyable: true + .. literalinclude:: /includes/usage-examples/code-snippets/find-sync.rs + :language: rust + :emphasize-lines: 18 + :dedent: - .. input:: /includes/usage-examples/code-snippets/find-sync.rs - :language: rust - :dedent: +Output +~~~~~~ - .. output:: - :language: console - :visible: false +Select the :guilabel:`BSON Document Results` or :guilabel:`Restaurant Struct Results` tab to +see the corresponding code output based on your collection's type parameter: + +.. tabs:: - // Results truncated - ... - Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" } - Restaurant { name: "Calliope", cuisine: "French" } - ... + .. tab:: BSON Document Results + :tabid: find-one-async + + .. code-block:: none + :copyable: false + + ... + Some( + Document({ + "_id": ObjectId( + "...", + ), + ... + "name": String( + "Cafe Un Deux Trois", + ), + ... + }), + ), + Some( + Document({ + "_id": ObjectId( + "...", + ), + ... + "name": String( + "Calliope", + ), + ... + }), + ) + ... + + .. tab:: Restaurant Struct Results + :tabid: find-one-sync + + .. code-block:: none + :copyable: false + + ... + Restaurant { + name: "Cafe Un Deux Trois", + cuisine: "French", + } + Restaurant { + name: "Calliope", + cuisine: "French", + } + ... \ No newline at end of file diff --git a/source/usage-examples/findOne.txt b/source/usage-examples/findOne.txt index d34156bd..d3d0a2af 100644 --- a/source/usage-examples/findOne.txt +++ b/source/usage-examples/findOne.txt @@ -4,6 +4,19 @@ Find a Document =============== +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: first, retrieve, code example + +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + You can retrieve a single document from a collection by calling the `find_one() <{+api+}/struct.Collection.html#method.find_one>`__ method on a ``Collection`` instance. @@ -22,12 +35,16 @@ Example ------- This example retrieves a document that matches a query filter from the ``restaurants`` -collection in the ``sample_restaurants`` database. The example populates a ``Restaurant`` -struct with data from the retrieved document. +collection in the ``sample_restaurants`` database. The ``find_one()`` method returns the +first document in which the value of the ``name`` field is ``"Tompkins Square Bagels"``. -This example uses a query filter that matches documents in which the value of the -``name`` field is ``"Tompkins Square Bagels"``. MongoDB retrieves the -first document that matches the query filter. +You can model the retrieved document as a BSON data 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: + +- ````: Retrieves and prints collection documents as BSON documents +- ````: Retrieves and prints 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: @@ -37,41 +54,55 @@ see the corresponding code for each runtime: .. tab:: Asynchronous :tabid: find-one-async - .. io-code-block:: - :copyable: true + .. literalinclude:: /includes/usage-examples/code-snippets/find-one-async.rs + :language: rust + :emphasize-lines: 20 + :dedent: + + .. tab:: Synchronous + :tabid: find-one-sync - .. input:: /includes/usage-examples/code-snippets/find-one-async.rs - :language: rust - :dedent: + .. literalinclude:: /includes/usage-examples/code-snippets/find-one-sync.rs + :language: rust + :emphasize-lines: 18 + :dedent: - .. output:: - :language: console - :visible: false +Output +~~~~~~ - Some( - Restaurant { - name: "Tompkins Square Bagels", - cuisine: "American", - }, - ) +Select the :guilabel:`BSON Document Result` or :guilabel:`Restaurant Struct Result` tab to +see the corresponding code output based on your collection's type parameter: - .. tab:: Synchronous - :tabid: find-one-sync +.. tabs:: - .. io-code-block:: - :copyable: true + .. tab:: BSON Document Result + :tabid: find-one-async - .. input:: /includes/usage-examples/code-snippets/find-one-sync.rs - :language: rust - :dedent: + .. code-block:: none + :copyable: false + + Some( + Document({ + "_id": ObjectId( + "...", + ), + ... + "name": String( + "Tompkins Square Bagels", + ), + ... + }), + ) + + .. tab:: Restaurant Struct Result + :tabid: find-one-sync - .. output:: - :language: console - :visible: false + .. code-block:: none + :copyable: false - Some( - Restaurant { - name: "Tompkins Square Bagels", - cuisine: "American", - }, - ) + Some( + Restaurant { + name: "Tompkins Square Bagels", + cuisine: "American", + }, + ) \ No newline at end of file