Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions source/includes/usage-examples/code-snippets/count-async.rs
Original file line number Diff line number Diff line change
@@ -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 = "<connection string>";

let client = Client::with_uri_str(uri).await?;
let my_coll: Collection<Document> = client

// Replace <T> with the <Document> or <Restaurant> type parameter
let my_coll: Collection<T> = client
.database("sample_restaurants")
.collection("restaurants");

Expand Down
12 changes: 10 additions & 2 deletions source/includes/usage-examples/code-snippets/count-sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<connection string>";

let client = Client::with_uri_str(uri)?;
let my_coll: Collection<Document> = client

// Replace <T> with the <Document> or <Restaurant> type parameter
let my_coll: Collection<T> = client
.database("sample_restaurants")
.collection("restaurants");

Expand Down
13 changes: 11 additions & 2 deletions source/includes/usage-examples/code-snippets/distinct-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<connection string>";

let client = Client::with_uri_str(uri).await?;
let my_coll: Collection<Document> = client

// Replace <T> with the <Document> or <Restaurant> type parameter
let my_coll: Collection<T> = client
.database("sample_restaurants")
.collection("restaurants");

Expand Down
13 changes: 11 additions & 2 deletions source/includes/usage-examples/code-snippets/distinct-sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<connection string>";

let client = Client::with_uri_str(uri)?;
let my_coll: Collection<Document> = client

// Replace <T> with the <Document> or <Restaurant> type parameter
let my_coll: Collection<T> = client
.database("sample_restaurants")
.collection("restaurants");

Expand Down
18 changes: 13 additions & 5 deletions source/usage-examples/count.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<T>`` type parameter on the highlighted
line with one of the following values:

- ``<Document>``: Represents collection documents as BSON documents
- ``<Restaurant>``: 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:
Expand All @@ -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::
Expand All @@ -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::
Expand Down
14 changes: 12 additions & 2 deletions source/usage-examples/distinct.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<T>`` type parameter on the highlighted
line with one of the following values:

- ``<Document>``: Represents collection documents as BSON documents
- ``<Restaurant>``: 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:

Expand All @@ -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::
Expand All @@ -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::
Expand Down
Loading