Advertisement
Guest User

Untitled

a guest
May 21st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. //!
  2. //! Here is the connector of mongo
  3. //!
  4. //!
  5. use bson::Bson;
  6. use bson::oid::ObjectId;
  7. use mongodb::{Client, ThreadedClient};
  8. use mongodb::db::ThreadedDatabase;
  9.  
  10.  
  11. ///
  12. /// This function will return abstract of your input
  13. ///
  14. ///
  15. pub fn greeting(host: &str, port: u16, dbname: &str) { //, username: &str, pass: &str
  16. let client = Client::connect(host, port)
  17. .ok().expect("Failed to initialize standalone client.");
  18.  
  19. println!("connected.. {}:{}", host, port);
  20. let db = client.db(dbname);
  21. println!("use db: {}", dbname);
  22.  
  23. // if (username.len() > 0) {
  24. // db.auth(username, pass).ok().expect("Failed to authorize user.");
  25. // }
  26.  
  27. let coll = db.collection("test");
  28.  
  29. println!("connect to test");
  30.  
  31. let myOid = ObjectId::with_string("5740269030a3783b30c9fc3f").unwrap();
  32. let doc = doc!{ "_id" => myOid };
  33.  
  34. // let doc = doc!{ "title" => "Jaws",
  35. // "array" => [ 1, 2, 3 ] };
  36.  
  37. /*
  38. // Insert document into 'test.movies' collection
  39. coll.insert_one(doc.clone(), None)
  40. .ok().expect("Failed to insert document.");
  41.  
  42. println!("insert doc");
  43. */
  44.  
  45. // Find the document and receive a cursor
  46. let mut cursor = coll.find(Some(doc.clone()), None)
  47. .ok().expect("Failed to execute find.");
  48.  
  49. println!("find docs");
  50.  
  51. let item = cursor.next();
  52.  
  53. println!("cursor->next");
  54.  
  55. // cursor.next() returns an Option<Result<Document>>
  56. match item {
  57. Some(Ok(doc)) => match doc.get("title") {
  58. Some(&Bson::String(ref title)) => println!("{}", title),
  59. _ => panic!("Expected title to be a string!"),
  60. },
  61. Some(Err(_)) => panic!("Failed to get next from server!"),
  62. None => panic!("Server returned no results!"),
  63. }
  64. println!("all done")
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement