From 40098478b97282f6f514360290b90b8603d7943e Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 22 May 2023 11:24:11 +0100 Subject: [PATCH] Updated use of credential builder to make code more efficient and removed unrelated JSON file --- src/main.rs | 14 ++++++++------ src/test.json | 19 ------------------- 2 files changed, 8 insertions(+), 25 deletions(-) delete mode 100644 src/test.json diff --git a/src/main.rs b/src/main.rs index 112d487..fc26503 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,19 +11,21 @@ async fn main() { // Manually set an option. client_options.app_name = Some("My App".to_string()); - client_options.credential = Some(Credential::default()); - client_options.credential.as_mut().unwrap().username = Some(std::env::var("mongo_username").unwrap()); - client_options.credential.as_mut().unwrap().password = Some(std::env::var("mongo_password").unwrap()); + + let credential_builder = Credential::builder() + .username(Some(std::env::var("mongo_username").unwrap())) + .password(Some(std::env::var("mongo_password").unwrap())); + + client_options.credential = Some(credential_builder.build()); // Get a handle to the deployment. let client = Client::with_options(client_options).unwrap(); - let db = client.database("test"); - match db.create_collection("test", None).await { Err(_) => panic!("ERROR: incorrect usage"), _ => {} - } + } + let collection = db.collection::("test"); loop { diff --git a/src/test.json b/src/test.json deleted file mode 100644 index 4bfdb82..0000000 --- a/src/test.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "id": 1, - "first_name": "Tom", - "last_name": "Cruise", - "photo": "https://jsonformatter.org/img/tom-cruise.jpg" -}, -{ - "id": 2, - "first_name": "Maria", - "last_name": "Sharapova", - "photo": "https://jsonformatter.org/img/Maria-Sharapova.jpg - " -}, -{ - "id": 3, - "first_name": "Robert", - "last_name": "Downey Jr", - "photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg" -} \ No newline at end of file