Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //!
- //! Here is the connector of mongo
- //!
- //!
- use bson::Bson;
- use bson::oid::ObjectId;
- use mongodb::{Client, ThreadedClient};
- use mongodb::db::ThreadedDatabase;
- ///
- /// This function will return abstract of your input
- ///
- ///
- pub fn greeting(host: &str, port: u16, dbname: &str) { //, username: &str, pass: &str
- let client = Client::connect(host, port)
- .ok().expect("Failed to initialize standalone client.");
- println!("connected.. {}:{}", host, port);
- let db = client.db(dbname);
- println!("use db: {}", dbname);
- // if (username.len() > 0) {
- // db.auth(username, pass).ok().expect("Failed to authorize user.");
- // }
- let coll = db.collection("test");
- println!("connect to test");
- let myOid = ObjectId::with_string("5740269030a3783b30c9fc3f").unwrap();
- let doc = doc!{ "_id" => myOid };
- // 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.");
- println!("insert doc");
- */
- // Find the document and receive a cursor
- let mut cursor = coll.find(Some(doc.clone()), None)
- .ok().expect("Failed to execute find.");
- println!("find docs");
- let item = cursor.next();
- println!("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!"),
- }
- println!("all done")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement