This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn delete(&self, doc: Document) -> Result<i64, Error> { | |
let collection = &self | |
.client | |
.get_db(&self.database) | |
.get_collection(&self.collection); | |
match collection.delete_one(doc, None) { | |
Ok(result) => Ok(result.deleted_count), | |
Err(e) => Err(e), | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let delete_doc = doc! {"title" :"Pay AmeX bill"}; | |
let delete_result = mongo_client.delete(delete_doc).unwrap(); | |
println!("Docs deleted: {:?}", delete_result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn update(&self, filter: Document, update: Document) -> Result<Option<i64>, Error> { | |
let collection = &self | |
.client | |
.get_db(&self.database) | |
.get_collection(&self.collection); | |
match collection.update_one(filter, update, None) { | |
Ok(result) => Ok(Some(result.modified_count)), | |
Err(e) => Err(e), | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let task_update = InsertableTask::new( | |
"Pay AmeX bill".to_string(), | |
"Bill".to_string(), | |
UtcDateTime(Utc::now()), | |
UtcDateTime(Utc.ymd(2020, 04, 28).and_hms(12, 0, 9)), | |
true, | |
); | |
let update_doc: Document = task_update.into(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn read(&self, doc: Document) -> Result<Option<Document>, Error> { | |
let collection = &self | |
.client | |
.get_db(&self.database) | |
.get_collection(&self.collection); | |
collection.find_one(Some(doc), None) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// document key to filter on | |
let doc_filter = doc! {"title": "Pay AmeX bill"}; | |
let read_doc = mongo_client.read(doc_filter); | |
if let Ok(doc) = read_doc { | |
println!("{:?}", doc.unwrap()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn create(&self, doc: Document) -> Result<Bson, Error> { | |
let collection = &self | |
.client | |
.get_db(&self.database) | |
.get_collection(&self.collection); | |
// inserts the given document in the collection | |
match collection.insert_one(doc, None) { | |
Ok(result) => Ok(result.inserted_id), | |
Err(e) => Err(e), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let task = InsertableTask::new( | |
"Pay AmeX bill".to_string(), | |
"Bill".to_string(), | |
UtcDateTime(Utc::now()), | |
UtcDateTime(Utc.ymd(2020, 04, 28).and_hms(12, 0, 9)), | |
false, | |
); | |
let document: Document = task.into(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let db_name = "rust-cosmos-demo"; | |
let collection_name = "tasks"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl MongoClient { | |
pub fn connect(connection_string: &str) -> Result<Self, Error> { | |
/// parses the connection string and extract host information, token, tls configuration etc. | |
let client_options = ClientOptions::parse(connection_string)?; | |
/// Initialize a connection to Cosmos DB's mongo server | |
let client = Client::with_options(client_options)?; | |
Ok(Self { client }) | |
} | |
} |
NewerOlder