Crate mongod

source ·
Expand description

§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 bson::Document;
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);
    }
}

T ### 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§

  • A blocking Client API.
  • Extensions on the bson & mongodb crates.
  • The query operations that can be perfomed on a MongoDB.

Structs§

  • An asynchronous Client to query mongo with.
  • A ClientBuilder can be used to create a Client with custom configuration.
  • The errors that may occur when talking to mongo.
  • A helper type to create a typed dictionary of sorted fields.
  • A typed cursor.
  • Used for complex updates using MongoDB’s update operators.

Enums§

  • The BSON comparators for comparison of different BSON type values
  • The Kind of mongod::Error.
  • The order in which to sort a field by.

Traits§

  • Used to tie a type implementing Collection to its companion Field type.
  • Used to tie a type implementing Collection to its companion Filter type.
  • Used to tie a type implementing Collection to its companion Update type.
  • Used to create a mongo collection from a type.
  • Used to mark an enum as a viable type for use in sorting.
  • Used to mark a type as a filter for use in queries.
  • Used to mark a type as an update for use in queries.