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 is 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/latest) crate for BSON support. The driver contains a fully async API that requires [`tokio`](https://docs.rs/tokio). The driver also has a sync API that may be enabled via feature flags. The MongoDB Rust driver follows [semantic versioning](https://semver.org/) for its releases.
5
6
6
7
For more details, including features, runnable examples, troubleshooting resources, and more, please see the [official documentation](https://www.mongodb.com/docs/drivers/rust/current/).
7
8
8
9
## Installation
10
+
9
11
### Requirements
12
+
10
13
- Rust 1.82.0+ (See the [MSRV policy](#minimum-supported-rust-version-msrv-policy) for more information)
11
14
- MongoDB 4.0+
12
15
@@ -15,7 +18,9 @@ For more details, including features, runnable examples, troubleshooting resourc
15
18
The driver tests against Linux, MacOS, and Windows in CI.
16
19
17
20
### Importing
21
+
18
22
The driver is available on [crates.io](https://crates.io/crates/mongodb). To use the driver in your application, simply add it to your project's `Cargo.toml`.
23
+
19
24
```toml
20
25
[dependencies]
21
26
mongodb = "3.2.3"
@@ -24,35 +29,45 @@ mongodb = "3.2.3"
24
29
Version 1 of this crate has reached end of life and will no longer be receiving any updates or bug fixes, so all users are recommended to always depend on the latest 2.x release. See the [2.0.0 release notes](https://github.com/mongodb/mongo-rust-driver/releases/tag/v2.0.0) for migration information if upgrading from a 1.x version.
25
30
26
31
#### Enabling the sync API
32
+
27
33
The driver also provides a blocking sync API. To enable this, add the `"sync"` feature to your `Cargo.toml`:
34
+
28
35
```toml
29
36
[dependencies.mongodb]
30
37
version = "3.2.3"
31
38
features = ["sync"]
32
39
```
40
+
33
41
**Note:** The sync-specific types can be imported from `mongodb::sync` (e.g. `mongodb::sync::Client`).
|`dns-resolver`| Enable DNS resolution to allow `mongodb+srv` URI handling. **Enabled by default.**|
40
-
|`rustls-tls`| Use [`rustls`](https://docs.rs/rustls/latest/rustls/) for TLS connection handling. **Enabled by default.**|
41
-
|`openssl-tls`| Use [`openssl`](https://docs.rs/openssl/latest/openssl/) for TLS connection handling. |
42
-
|`sync`| Expose the synchronous API (`mongodb::sync`). |
43
-
|`aws-auth`| Enable support for the MONGODB-AWS authentication mechanism. |
44
-
|`zlib-compression`| Enable support for compressing messages with [`zlib`](https://zlib.net/). |
45
-
|`zstd-compression`| Enable support for compressing messages with [`zstd`](http://facebook.github.io/zstd/). |
46
-
|`snappy-compression`| Enable support for compressing messages with [`snappy`](http://google.github.io/snappy/). |
47
-
|`in-use-encryption`| Enable support for client-side field level encryption and queryable encryption. Note that re-exports from the `mongocrypt` crate may change in backwards-incompatible ways while that crate is below version 1.0. |
48
-
|`tracing-unstable`| Enable support for emitting [`tracing`](https://docs.rs/tracing/latest/tracing/) events. This API is unstable and may be subject to breaking changes in minor releases. |
49
-
|`compat-3-0-0`| Required for future compatibility if default features are disabled. |
|`dns-resolver`| Enable DNS resolution to allow `mongodb+srv` URI handling. **Enabled by default.**|
48
+
|`rustls-tls`| Use [`rustls`](https://docs.rs/rustls/latest/rustls/) for TLS connection handling. **Enabled by default.**|
49
+
|`openssl-tls`| Use [`openssl`](https://docs.rs/openssl/latest/openssl/) for TLS connection handling.|
50
+
|`sync`| Expose the synchronous API (`mongodb::sync`).|
51
+
|`aws-auth`| Enable support for the MONGODB-AWS authentication mechanism.|
52
+
|`zlib-compression`| Enable support for compressing messages with [`zlib`](https://zlib.net/).|
53
+
|`zstd-compression`| Enable support for compressing messages with [`zstd`](http://facebook.github.io/zstd/).|
54
+
|`snappy-compression`| Enable support for compressing messages with [`snappy`](http://google.github.io/snappy/).|
55
+
|`in-use-encryption`| Enable support for client-side field level encryption and queryable encryption. Note that re-exports from the `mongocrypt` crate may change in backwards-incompatible ways while that crate is below version 1.0. |
56
+
|`tracing-unstable`| Enable support for emitting [`tracing`](https://docs.rs/tracing/latest/tracing/) events. This API is unstable and may be subject to breaking changes in minor releases.|
57
+
|`compat-3-0-0`| Required for future compatibility if default features are disabled.|
50
58
51
59
## Web Framework Examples
60
+
52
61
### Actix
62
+
53
63
The driver can be used easily with the Actix web framework by storing a `Client` in Actix application data. A full example application for using MongoDB with Actix can be found [here](https://github.com/actix/examples/tree/master/databases/mongodb).
54
64
65
+
### Axum
66
+
67
+
A simple CRUD API example using Axum and MongoDB can be found [here](https://github.com/tokio-rs/axum/tree/main/examples/mongodb).
68
+
55
69
### Rocket
70
+
56
71
The Rocket web framework provides built-in support for MongoDB via the Rust driver. The documentation for the [`rocket_db_pools`](https://api.rocket.rs/v0.5/rocket_db_pools/index.html) crate contains instructions for using MongoDB with your Rocket application.
57
72
58
73
## Note on connecting to Atlas deployments
@@ -78,7 +93,9 @@ the driver's futures directly. This will ensure the driver's futures will always
78
93
while also allowing the application to continue in the event of a timeout.
79
94
80
95
## Bug Reporting / Feature Requests
96
+
81
97
To file a bug report or submit a feature request, please open a ticket on our [Jira project](https://jira.mongodb.org/browse/RUST):
98
+
82
99
- Create an account and login at [jira.mongodb.org](https://jira.mongodb.org)
83
100
- Navigate to the RUST project at [jira.mongodb.org/browse/RUST](https://jira.mongodb.org/browse/RUST)
84
101
- Click **Create Issue** - If the ticket you are filing is a bug report, please include as much detail as possible about the issue and how to reproduce it.
@@ -90,29 +107,38 @@ Before filing a ticket, please use the search functionality of Jira to see if a
90
107
We encourage and would happily accept contributions in the form of GitHub pull requests. Before opening one, be sure to run the tests locally; check out the [testing section](#running-the-tests) for information on how to do that. Once you open a pull request, your branch will be run against the same testing matrix that we use for our [continuous integration](#continuous-integration) system, so it is usually sufficient to only run the integration tests locally against a standalone. Remember to always run the linter tests before opening a pull request.
91
108
92
109
## Running the tests
110
+
93
111
### Integration and unit tests
112
+
94
113
In order to run the tests (which are mostly integration tests), you must have access to a MongoDB deployment. You may specify a [MongoDB connection string](https://www.mongodb.com/docs/manual/reference/connection-string/) in the `MONGODB_URI` environment variable, and the tests will use it to connect to the deployment. If `MONGODB_URI` is unset, the tests will attempt to connect to a local deployment on port 27017.
95
114
96
115
**Note:** The integration tests will clear out the databases/collections they need to use, but they do not clean up after themselves.
97
116
98
117
To actually run the tests, you can use `cargo` like you would in any other crate:
118
+
99
119
```bash
100
120
cargo test --verbose # runs against localhost:27017
101
121
export MONGODB_URI="mongodb://localhost:123"
102
122
cargo test --verbose # runs against localhost:123
103
123
```
104
124
105
125
#### Auth tests
126
+
106
127
The authentication tests will only be included in the test run if certain requirements are met:
128
+
107
129
- The deployment must have `--auth` enabled
108
130
- Credentials must be specified in `MONGODB_URI`
109
131
- The credentials specified in `MONGODB_URI` must be valid and have root privileges on the deployment
Certain tests will only be run against certain topologies. To ensure that the entire test suite is run, make sure to run the tests separately against standalone, replicated, and sharded deployments.
141
+
116
142
```bash
117
143
export MONGODB_URI="mongodb://my-standalone-host:27017"# mongod running on 27017
118
144
cargo test --verbose
@@ -123,28 +149,36 @@ cargo test --verbose
123
149
```
124
150
125
151
#### Run the tests with TLS/SSL
152
+
126
153
To run the tests with TLS/SSL enabled, you must enable it on the deployment and in `MONGODB_URI`.
**Note:** When you open a pull request, your code will be run against a comprehensive testing matrix, so it is usually not necessary to run the integration tests against all combinations of topology/auth/TLS locally.
132
161
133
162
### Linter Tests
163
+
134
164
Our linter tests use the nightly version of `rustfmt` to verify that the source is formatted properly and the stable version of `clippy` to statically detect any common mistakes.
135
165
You can use `rustup` to install them both:
166
+
136
167
```bash
137
168
rustup component add clippy --toolchain stable
138
169
rustup component add rustfmt --toolchain nightly
139
170
```
171
+
140
172
Our linter tests also use `rustdoc` to verify that all necessary documentation is present and properly formatted. `rustdoc` is included in the standard Rust distribution.
141
173
142
174
To run the linter tests, run the `check-clippy.sh`, `check-rustfmt.sh`, and `check-rustdoc.sh` scripts in the `.evergreen` directory. To run all three, use the `check-all.sh` script.
175
+
143
176
```bash
144
177
bash .evergreen/check-all.sh
145
178
```
146
179
147
180
## Continuous Integration
181
+
148
182
Commits to main are run automatically on [evergreen](https://evergreen.mongodb.com/waterfall/mongo-rust-driver).
0 commit comments