Skip to content

Instantly share code, notes, and snippets.

View gdhuper's full-sized avatar

Gurpreet Singh gdhuper

View GitHub Profile
@gdhuper
gdhuper / db.rs
Created April 22, 2020 22:37
cosmos-rust-getting-started-db-delete
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),
   }
@gdhuper
gdhuper / main.rs
Created April 22, 2020 22:37
cosmos-rust-getting-started-main-delete
let delete_doc = doc! {"title" :"Pay AmeX bill"};
let delete_result = mongo_client.delete(delete_doc).unwrap();
println!("Docs deleted: {:?}", delete_result);
@gdhuper
gdhuper / db.rs
Created April 22, 2020 22:36
cosmos-rust-getting-started-db-update
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),
   }
@gdhuper
gdhuper / main.rs
Created April 22, 2020 22:35
cosmos-rust-getting-started-main-update
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();
@gdhuper
gdhuper / db.rs
Created April 22, 2020 22:30
cosmos-rust-getting-started-db-read
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)
}
@gdhuper
gdhuper / main.rs
Created April 22, 2020 22:29
cosmos-rust-getting-started-main-read
/// 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());
}
@gdhuper
gdhuper / db.rs
Created April 22, 2020 22:25
cosmos-rust-getting-started-db-create
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),
@gdhuper
gdhuper / main.rs
Created April 22, 2020 22:24
cosmos-rust-getting-started-create
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();
@gdhuper
gdhuper / main.rs
Created April 22, 2020 22:22
cosmos-rust-getting-started-db-collection
let db_name = "rust-cosmos-demo";
let collection_name = "tasks";
@gdhuper
gdhuper / db.rs
Created April 22, 2020 22:21
cosmos-rust-getting-started-db-client
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 })
      }
}