Uploaded image for project: 'Rust Driver'
  1. Rust Driver
  2. RUST-621

MongoDB doesn't save document when timestamps are initialized in struct

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: Trivial - P5
    • Resolution: Gone away
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:

      Description

      I am having a very weird issue when trying to save a data in Mongodb by the Rust driver. This is my struct

      // #[derive(Deserialize, Serialize, Debug)]
      struct Info {
          #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
          id: Option<bson::oid::ObjectId>,
          name: String,
          created_at: i64,
          updated_at: i64,
      }

      And this is my actix route handler function

      // async fn post_request(info: web::Json<Info>, data: web::Data<State>) -> impl Responder {
          let name: &str = &info.name;
          let document = Info {
              id: None,
              name: name.to_string(),
              created_at: Utc::now().timestamp_millis(),
              updated_at: Utc::now().timestamp_millis(),
          };
          // Convert to a Bson instance:
          let serialized_doc = bson::to_bson(&document).unwrap();
          let doc = serialized_doc.as_document().unwrap();
          let collection = data.client.database("test1").collection("users");
          let result = collection.insert_one(doc.to_owned(), None).await.unwrap();
          HttpResponse::Ok().json(result).await
      }

      I am getting the Utc struct by chrono crate. When i am trying to save the data in MongoDB by hitting the route, It doesn't gets saved. But, oddly, when i comment out the created_at and updated_at in struct and handler, it gets saved. If i don't use structs and try to save it as a raw document, by storing created_at and updated_at in variables, then also it gets saved, but not by structs. I am new to rust so maybe i am doing something wrong. 

      This is my Cargo.toml

      // [dependencies]
      mongodb = { version = "1.1.1", default-features = false, features=["async-std-runtime"] }
      actix-web = "3.3.2"
      dotenv = "0.15.0"
      serde = "1.0.118"
      bson = "1.1.0"
      chrono = { version = "0.4.19", features = ["serde"] }
      futures = "0.3.8"
      

      Please help

        Attachments

          Activity

            People

            Assignee:
            sam.rossi Samuel Rossi (Inactive)
            Reporter:
            mongojira@mailinator.com Alonzo A
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: