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
}
And 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 = {version = "1.0.118", features = ["derive"]}
bson = "1.1.0"
chrono = { version = "0.4.19", features = ["serde"] }
futures = "0.3.8"
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 using the doc!
macro and 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. Please help