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![ ServerAddress::Tcp { host: "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 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.
syncContains 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.
A MongoDB client session. This struct represents a logical session used for ordering sequential
operations. To create a ClientSession, call start_session on a Client.
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, the returned Cursor will
contain the first batch of results from the server; the individual results will then 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
next. Because of this, a Cursor iterates over Result<T> items rather than
simply T 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.
A SessionCursor is a cursor that was created with a ClientSession that must be iterated
using one. To iterate, use SessionCursor::next or retrieve a SessionCursorStream using
SessionCursor::stream:
A type that implements Stream which can be used to
stream the results of a SessionCursor. Returned from SessionCursor::stream.