Crate mongodb[][src]

Expand description

This crate is a pure Rust MongoDB driver. It follows the MongoDB driver API and feature specifications.

To connect to a MongoDB database, pass a MongoDB connection string to Client::with_uri_str:

let client = Client::with_uri_str("mongodb://localhost:27017/").await?;

Alternately, create an instance of ClientOptions and pass it to Client::with_options:

let options = ClientOptions::builder()
                  .hosts(vec![
                      StreamAddress {
                          hostname: "localhost".into(),
                          port: Some(27017),
                      }
                  ])
                  .build();

let client = Client::with_options(options)?;

Operations can be performed by obtaining a Database or Collection from the Client:

let db = client.database("some_db");
for coll_name in db.list_collection_names(None).await? {
    println!("collection: {}", coll_name);
}

let coll = db.collection("some-coll");
let result = coll.insert_one(doc! { "x": 1 }, None).await?;
println!("{:#?}", result);

Re-exports

pub use {{root}}::bson;

Modules

Contains the Error and Result types that mongodb uses.

Contains the events and functionality for monitoring internal Client behavior.

Contains all of the types needed to specify options to MongoDB operations.

Contains the types of results returned by CRUD operations.

syncsync

Contains the sync API. This is only available when the sync feature is enabled.

Structs

This is the main entry point for the API. A Client is used to connect to a MongoDB cluster. By default, it will monitor the topology of the cluster, keeping track of any changes, such as servers being added or removed.

Collection is the client-side abstraction of a MongoDB Collection. It can be used to perform collection-level operations such as CRUD operations. A Collection can be obtained through a Database by calling either Database::collection or Database::collection_with_options.

A Cursor streams the result of a query. When a query is made, a Cursor will be returned with the first batch of results from the server; the documents will be returned as the Cursor is iterated. When the batch is exhausted and if there are more results, the Cursor will fetch the next batch of documents, and so forth until the results are exhausted. Note that because of this batching, additional network I/O may occur on any given call to Cursor::next. Because of this, a Cursor iterates over Result<Document> items rather than simply Document items.

Database is the client-side abstraction of a MongoDB database. It can be used to perform database-level operations or to obtain handles to specific collections within the database. A Database can only be obtained through a Client by calling either Client::database or Client::database_with_options.

A struct modeling the canonical name for a collection in MongoDB.