You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository contains the officially supported MongoDB Rust driver, a client side library that can be used to interact with MongoDB deployments in Rust applications. It uses the [`bson`](https://docs.rs/bson) crate for BSON support. The driver contains a fully async API that supports either [`tokio`](https://crates.io/crates/tokio) (default) or [`async-std`](https://crates.io/crates/async-std), depending on the feature flags set. The driver also has a sync API that may be enabled via feature flag.
4
+
This repository contains the officially supported MongoDB Rust driver, a client side library that can be used to interact with MongoDB deployments in Rust applications. It uses the [`bson`](https://docs.rs/bson/2.0.0-beta) crate for BSON support. The driver contains a fully async API that supports either [`tokio`](https://crates.io/crates/tokio) (default) or [`async-std`](https://crates.io/crates/async-std), depending on the feature flags set. The driver also has a sync API that may be enabled via feature flag.
5
5
6
6
## Index
7
7
-[Installation](#installation)
@@ -63,7 +63,7 @@ features = ["sync"]
63
63
**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`).
64
64
65
65
## Example Usage
66
-
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).
66
+
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).
67
67
68
68
### Using the async API
69
69
#### Connecting to a MongoDB deployment
@@ -112,32 +112,64 @@ let docs = vec can be parameterized with any type that implements the `Serialize` and `Deserialize` traits from the [`serde`](https://serde.rs/) crate, not just `Document`:
117
+
118
+
```toml
119
+
# In Cargo.toml, add the following dependency.
120
+
serde = { version = "1.0", features = ["derive"] }
// Insert the books into "mydb.books" collection, no manual conversion to BSON necessary.
149
+
typed_collection.insert_many(books, None).await?;
150
+
```
151
+
115
152
#### Finding documents in a collection
153
+
Results from queries are generally returned via [`Cursor`](https://docs.rs/mongodb/2.0.0-beta/mongodb/struct.Cursor.html), a struct which streams the results back from the server as requested. The [`Cursor`](https://docs.rs/mongodb/2.0.0-beta/mongodb/struct.Cursor.html) type implements the [`Stream`](https://docs.rs/futures/latest/futures/stream/index.html) trait from the [`futures`](https://crates.io/crates/futures) crate, and in order to access its streaming functionality you need to import at least one of the [`StreamExt`](https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html) or [`TryStreamExt`](https://docs.rs/futures/latest/futures/stream/trait.TryStreamExt.html) traits.
154
+
155
+
```toml
156
+
# In Cargo.toml, add the following dependency.
157
+
futures = "0.3"
158
+
```
116
159
```rust
117
-
usefutures::stream::StreamExt;
118
-
usemongodb::{
119
-
bson::{doc, Bson},
120
-
options::FindOptions,
121
-
};
160
+
// This trait is required to use `try_next()` on the cursor
161
+
usefutures::stream::TryStreamExt;
162
+
usemongodb::{bson::doc, options::FindOptions};
122
163
```
123
164
```rust
124
-
// Query the documents in the collection with a filter and an option.
165
+
// Query the books in the collection with a filter and an option.
@@ -147,36 +179,43 @@ The driver also provides a blocking sync API. See the [Installation](#enabling-t
147
179
The various sync-specific types are found in the `mongodb::sync` submodule rather than in the crate's top level like in the async API. The sync API calls through to the async API internally though, so it looks and behaves similarly to it.
0 commit comments