mongod 0.3.1

An abstraction layer on mongodb
Documentation

Mongo

The mongod crate aims to provide a convenient, higher-level wrapper on top of the official bson & mongodb crates.

It provides the following:

  • Async and blocking Clients
  • Bson extensions
  • Strong typing through the use of traits

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.

# mod wrapper {
# use mongod_derive::{Bson, Mongo};
#[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 collection
  • Find: Fetch documents from a collection
  • Insert: Insert documents into a collection
  • Replace: Replace documents in a collection
  • Update: Update documents in a collection

Deleting

Deleting a user from the users collection.

# mod wrapper {
# use mongod_derive::{Bson, Mongo};
# #[derive(Bson, Mongo)]
# #[mongo(collection="users", field, filter, update)]
# pub struct User {
#     name: String,
#     age: Option<u32>,
#     email: Option<String>,
# }
# async fn doc() -> Result<(), mongod::Error> {
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);
# Ok(())
# }
# }

Fetching

Fetching users from the users collection.

# mod wrapper {
# use std::convert::TryFrom;
# use mongod_derive::{Bson, Mongo};
use bson::Document;
# #[derive(Debug, Bson, Mongo)]
# #[mongo(collection="users", field, filter, update)]
# pub struct User {
#     name: String,
#     age: Option<u32>,
#     email: Option<String>,
# }
# async fn doc() -> Result<(), mongod::Error> {
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((id, user)) = res {
println!("{} - {:?}", id, user);
}
}
# Ok(())
# }
# }

T ### Inserting

Inserting a user into the users collection.

# mod wrapper {
# use mongod_derive::{Bson, Mongo};
# #[derive(Debug, Bson, Mongo)]
# #[mongo(collection="users", field, filter, update)]
# pub struct User {
#     name: String,
#     age: Option<u32>,
#     email: Option<String>,
# }
# async fn doc() -> Result<(), mongod::Error> {
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);
# Ok(())
# }
# }

Replacing

Replacing a user in the users collection.

# mod wrapper {
# use mongod_derive::{Bson, Mongo};
# #[derive(Debug, Bson, Mongo)]
# #[mongo(collection="users", field, filter, update)]
# pub struct User {
#     name: String,
#     age: Option<u32>,
#     email: Option<String>,
# }
# async fn doc() -> Result<(), mongod::Error> {
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);
# Ok(())
# }
# }

Updating

Updating a user in the users collection.

# mod wrapper {
# use mongod_derive::{Bson, Mongo};
# #[derive(Debug, Bson, Mongo)]
# #[mongo(collection="users", field, filter, update)]
# pub struct User {
#     name: String,
#     age: Option<u32>,
#     email: Option<String>,
# }
# async fn doc() -> Result<(), mongod::Error> {
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);
# Ok(())
# }
# }

Optional Features

The following are a list of Cargo Features that cna be enabled or disabled: