Skip to content

Commit 8c4fb66

Browse files
authored
RUST-736 Update driver documentation for 2.0 release (#437)
1 parent 9e0f934 commit 8c4fb66

File tree

5 files changed

+282
-76
lines changed

5 files changed

+282
-76
lines changed

.evergreen/check-rustdoc.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
set -o errexit
44

55
. ~/.cargo/env
6-
cargo rustdoc -- -D warnings
7-
cargo rustdoc --no-default-features --features async-std-runtime -- -D warnings
8-
cargo rustdoc --no-default-features --features sync -- -D warnings
6+
cargo +nightly rustdoc -p bson --all-features -- --cfg docsrs -D warnings

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This repository contains the officially supported MongoDB Rust driver, a client
99
- [Importing](#importing)
1010
- [Configuring the async runtime](#configuring-the-async-runtime)
1111
- [Enabling the sync API](#enabling-the-sync-api)
12+
- [All feature flags](#all-feature-flags)
1213
- [Example Usage](#example-usage)
1314
- [Using the async API](#using-the-async-api)
1415
- [Connecting to a MongoDB deployment](#connecting-to-a-mongodb-deployment)
@@ -58,6 +59,17 @@ features = ["sync"]
5859
```
5960
**Note:** if the sync API is enabled, the async-specific types will be privatized (e.g. `mongodb::Client`). The sync-specific types can be imported from `mongodb::sync` (e.g. `mongodb::sync::Client`).
6061

62+
### All Feature Flags
63+
64+
| Feature | Description | Extra dependencies | Default |
65+
|:--------------------|:--------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------|:--------|
66+
| `tokio-runtime` | Enable support for the `tokio` async runtime | `tokio` 1.0 with the `full` feature | yes |
67+
| `async-std-runtime` | Enable support for the `async-std` runtime | `async-std` 1.0 | no |
68+
| `sync` | Expose the synchronous API (`mongodb::sync`). This flag cannot be used in conjunction with either of the async runtime feature flags. | `async-std` 1.0 | no |
69+
| `aws-auth` | Enable support for the MONGODB-AWS authentication mechanism. | `reqwest` 0.11 | no |
70+
| `bson-uuid-0_8` | Enable support for v0.8 of the [`uuid`](docs.rs/uuid/0.8) crate in the public API of the re-exported `bson` crate. | `uuid` 0.8 | no |
71+
| `bson-chrono-0_4` | Enable support for v0.4 of the [`chrono`](docs.rs/chrono/0.4) crate in the public API of the re-exported `bson` crate. | n/a | no |
72+
6173
## Example Usage
6274
Below are simple examples of using the driver. For more specific examples and the API reference, see the driver's [docs.rs page](https://docs.rs/mongodb/2.0.0-beta.3).
6375

src/coll/mod.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,19 @@ use crate::{
5858
/// [`Database::collection`](struct.Database.html#method.collection) or
5959
/// [`Database::collection_with_options`](struct.Database.html#method.collection_with_options).
6060
///
61+
/// A [`Collection`] can be parameterized with any type that implements the
62+
/// `Serialize` and `Deserialize` traits from the [`serde`](https://serde.rs/) crate. This includes but
63+
/// is not limited to just `Document`. The various methods that accept or return instances of the
64+
/// documents in the collection will accept/return instances of the generic parameter (e.g.
65+
/// [`Collection::insert_one`] accepts it as an argument, [`Collection::find_one`] returns an
66+
/// `Option` of it). It is recommended to define types that model your data which you can
67+
/// parameterize your [`Collection`]s with instead of `Document`, since doing so eliminates a lot of
68+
/// boilerplate deserialization code and is often more performant.
69+
///
6170
/// `Collection` uses [`std::sync::Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) internally,
62-
/// so it can safely be shared across threads or async tasks. For example:
71+
/// so it can safely be shared across threads or async tasks.
6372
///
73+
/// # Example
6474
/// ```rust
6575
/// # use mongodb::{
6676
/// # bson::doc,
@@ -76,14 +86,24 @@ use crate::{
7686
/// # use mongodb::Client;
7787
/// #
7888
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
79-
/// let coll = client.database("items").collection("in_stock");
89+
/// use serde::{Deserialize, Serialize};
90+
///
91+
/// /// Define a type that models our data.
92+
/// #[derive(Clone, Debug, Deserialize, Serialize)]
93+
/// struct Item {
94+
/// id: u32,
95+
/// }
96+
///
97+
/// // Parameterize our collection with the model.
98+
/// let coll = client.database("items").collection::<Item>("in_stock");
8099
///
81100
/// for i in 0..5 {
82101
/// let coll_ref = coll.clone();
83102
///
103+
/// // Spawn several tasks that operate on the same collection concurrently.
84104
/// task::spawn(async move {
85-
/// // Perform operations with `coll_ref`. For example:
86-
/// coll_ref.insert_one(doc! { "x": i }, None).await;
105+
/// // Perform operations with `coll_ref` that work with directly our model.
106+
/// coll_ref.insert_one(Item { id: i }, None).await;
87107
/// });
88108
/// }
89109
/// #

src/db/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,25 @@ impl Database {
130130
self.inner.write_concern.as_ref()
131131
}
132132

133-
/// Gets a handle to a collection with type `T` specified by `name` of the database. The
134-
/// `Collection` options (e.g. read preference and write concern) will default to those of the
135-
/// `Database`.
133+
/// Gets a handle to a collection in this database with the provided name. The
134+
/// [`Collection`] options (e.g. read preference and write concern) will default to those of
135+
/// this [`Database`].
136+
///
137+
/// For more information on how the generic parameter `T` is used, check out the [`Collection`]
138+
/// documentation.
136139
///
137140
/// This method does not send or receive anything across the wire to the database, so it can be
138141
/// used repeatedly without incurring any costs from I/O.
139142
pub fn collection<T>(&self, name: &str) -> Collection<T> {
140143
Collection::new(self.clone(), name, None)
141144
}
142145

143-
/// Gets a handle to a collection with type `T` specified by `name` in the cluster the `Client`
144-
/// is connected to. Operations done with this `Collection` will use the options specified by
145-
/// `options` by default and will otherwise default to those of the `Database`.
146+
/// Gets a handle to a collection in this database with the provided name.
147+
/// Operations done with this `Collection` will use the options specified by
148+
/// `options` and will otherwise default to those of this [`Database`].
149+
///
150+
/// For more information on how the generic parameter `T` is used, check out the [`Collection`]
151+
/// documentation.
146152
///
147153
/// This method does not send or receive anything across the wire to the database, so it can be
148154
/// used repeatedly without incurring any costs from I/O.

0 commit comments

Comments
 (0)