[−][src]Crate mongod
Mongo
The mongod
crate aims to provide a convenient, higher-level wrapper on top of the official
bson
& mongodb
crates.
It provides the following:
The mongod::Client
is asynchronous. For applications that need a synchronous
solution, the mongod::blocking
API can be used.
Making Collections
Defining an example collection using derive.
#[derive(Bson, Mongo)] #[mongo(collection="users", field, filter, update)] pub struct User { name: String, age: Option<u32>, email: Option<String>, }
Making requests
This crate is opinionated, here are some examples on how to use it for interaction with mongodb. For more complex interactions see the individual implementations:
Delete
: Delete documents from a collectionFind
: Fetch documents from a collectionInsert
: Insert documents into a collectionReplace
: Replace documents in a collectionUpdate
: Update documents in a collection
Deleting
Deleting a user from the users collection.
use mongod::{AsFilter, Comparator}; let client = mongod::Client::new(); let mut filter = User::filter(); filter.name = Some(Comparator::Eq("foo".to_owned())); let deleted = client.delete::<User, _>(Some(filter)).await.unwrap(); println!("delete {} documents", deleted);
Fetching
Fetching users from the users collection.
use futures::stream::StreamExt; use mongod::Collection; let client = mongod::Client::new(); let mut cursor = client.find::<User, _>(None).await.unwrap(); while let Some(res) = cursor.next().await { if let Ok(doc) = res { let user: User = User::from_document(doc).unwrap(); println!("{:?}", user); } }
Inserting
Inserting a user into the users collection.
let client = mongod::Client::new(); let user = User { name: "foo".to_owned(), age: None, email: None, }; let result = client.insert(vec![user]).await.unwrap(); println!("(index: oid) {:?}", result);
Replacing
Replacing a user in the users collection.
use mongod::{AsFilter, Comparator}; let client = mongod::Client::new(); let mut filter = User::filter(); filter.name = Some(Comparator::Eq("foo".to_owned())); let user = User { name: "foo".to_owned(), age: Some(0), email: None, }; let oid = client.replace_one(filter, user).await.unwrap(); println!("{:?}", oid);
Updating
Updating a user in the users collection.
use mongod::{AsFilter, Comparator, AsUpdate}; let client = mongod::Client::new(); let mut filter = User::filter(); filter.name = Some(Comparator::Eq("foo".to_owned())); let mut update = User::update(); update.age = Some(Some(0)); let updates = mongod::Updates { set: Some(update), ..Default::default() }; let updated = client.update::<User, _, _>(filter, updates).await.unwrap(); println!("updated {} documents", updated);
Optional Features
The following are a list of Cargo Features that cna be enabled or disabled:
- blocking: Provides the blocking client API.
- chrono: Provides the chrono support for the
ext::bson
. - derive: Provides the
derive
macros from the mongo-derive crate.
Re-exports
pub extern crate bson; |
pub extern crate mongodb as db; |
pub use self::query::Query; |
Modules
blocking | A blocking Client API. |
ext | Extensions on the |
query | The query operations that can be perfomed on a MongoDB. |
Structs
Client | An asynchronous |
ClientBuilder | A |
Error | The errors that may occur when talking to mongo. |
Sort | A helper type to create a typed dictionary of sorted fields. |
Updates | Used for complex updates using MongoDB's update operators. |
Enums
Comparator | The BSON comparators for comparison of different BSON type values |
ErrorKind | The |
Order | The order in which to sort a field by. |
Traits
AsField | Used to tie a type implementing |
AsFilter | Used to tie a type implementing |
AsUpdate | Used to tie a type implementing |
Collection | Used to create a mongo collection from a type. |
Field | Used to mark an |
Filter | Used to mark a type as a filter for use in queries. |
Update | Used to mark a type as an update for use in queries. |