[][src]Crate mongodb

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/")?;

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)? {
    println!("collection: {}", coll_name);
}

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

Modules

error

Contains the Error and Result types that mongodb uses.

event

Contains the events and functionality for monitoring internal Client behavior.

options

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

results

Contains the types of results returned by CRUD operations.

Structs

Client

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

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.

Cursor

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

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.

Namespace

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