[][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:

  • 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.

#[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.

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:

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 bson & mongodb crates.

query

The query operations that can be perfomed on a MongoDB.

Structs

Client

An asynchronous Client to query mongo with.

ClientBuilder

A ClientBuilder can be used to create a Client with custom configuration.

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 Kind of mongod::Error.

Order

The order in which to sort a field by.

Traits

AsField

Used to tie a type implementing Collection to its companion Field type.

AsFilter

Used to tie a type implementing Collection to its companion Filter type.

AsUpdate

Used to tie a type implementing Collection to its companion Update type.

Collection

Used to create a mongo collection from a type.

Field

Used to mark an enum as a viable type for use in sorting.

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.