This branch contains active development on a new driver written for Rust 1.0.
The API and implementation are currently subject to change at any time. You must not use this driver in production as it is still under development and is in no way supported by MongoDB Inc. We absolutely encourage you to experiment with it and provide us feedback on the API, design, and implementation. Bug reports and suggestions for improvements are welcomed, as are pull requests.
The 1.0 driver is available on crates.io. To use the MongoDB driver in your code, add the bson and mongodb packages to your Cargo.toml
:
[dependencies]
bson = "0.1.3"
mongodb = "0.1.0"
Then, import the bson and driver libraries within your code.
#[macro_use(bson, doc)]
extern crate bson;
extern crate mongodb;
Here's a basic example of driver usage:
use bson::Bson;
use mongodb::{Client, ThreadedClient};
use mongodb::db::ThreadedDatabase;
fn main() {
let client = Client::connect("localhost", 27017)
.ok().expect("Failed to initialize standalone client.");
let coll = client.db("test").collection("movies");
let doc = doc! { "title" => "Jaws",
"array" => [ 1, 2, 3 ] };
// Insert document into 'test.movies' collection
coll.insert_one(doc.clone(), None)
.ok().expect("Failed to insert document.");
// Find the document and receive a cursor
let mut cursor = coll.find(Some(doc.clone()), None)
.ok().expect("Failed to execute find.");
let item = cursor.next();
// cursor.next() returns an Option<Result<Document>>
match item {
Some(Ok(doc)) => match doc.get("title") {
Some(&Bson::String(ref title)) => println!("{}", title),
_ => panic!("Expected title to be a string!"),
},
Some(Err(_)) => panic!("Failed to get next from server!"),
None => panic!("Server returned no results!"),
}
}
Documentation is built using Cargo. The latest documentation can be found here. Generated documentation using cargo doc
can be found under the target/doc/ folder.