Advertisement
Guest User

Untitled

a guest
Dec 21st, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. extern crate mongodb;
  2. #[macro_use] extern crate bson;
  3.  
  4. use bson::{Bson, Document};
  5. use bson::oid::ObjectId;
  6.  
  7. use mongodb::{Client, ClientOptions, ThreadedClient};
  8. use mongodb::db::{Database, ThreadedDatabase};
  9. use mongodb::connstring::{ConnectionString, ConnectionOptions, Host};
  10. use mongodb::topology::{TopologyDescription, TopologyType};
  11.  
  12. fn main(){
  13. let user = "user";
  14. let password = "password";
  15. let host = "domain.name".to_string();
  16.  
  17. let hosts = vec![Host{
  18. host_name: host,
  19. ipc: String::new(),
  20. port: 27017
  21. }];
  22.  
  23. let connection_config = ConnectionString{
  24. hosts: hosts,
  25. string: None,
  26. user: Some(user.to_string()),
  27. password: Some(password.to_string()),
  28. database: None,
  29. collection: None,
  30. options: None
  31. };
  32.  
  33. let mut client_options = ClientOptions::new();
  34. client_options.log_file = Some("/tmp/mongo-commands".to_string());
  35.  
  36. let mut description = TopologyDescription::new();
  37. description.topology_type = TopologyType::Single;
  38.  
  39. let client = Client::with_config(
  40. connection_config,
  41. Some(client_options),
  42. Some(description)
  43. ).ok().expect("Couldn't connect to mongodb database");
  44.  
  45. let database = client.db("db");
  46. database.auth(user, password)
  47. .ok().expect("Can't authenticate to database");
  48.  
  49. let collection = database.collection("collection");
  50.  
  51. let mut doc = Document::new();
  52. doc.insert("test".to_owned(), Bson::String("test".to_string()));
  53.  
  54. collection.insert_one(doc, None).ok().expect("Couldn't insert a document");
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement