I am new to mongoDb, and I'm confused about this particular behavior:
I have a struct(see below for the struct) to create courses, with a chrono::Datetime field for the course schedule(course_datetime). When I create a new course and write it in mongodb with insert_one:
let resp = collection.insert_one(new_course, None).await?;
the course_datetime field is written as a String in the DB.(I would prefer for it to be stored as a ISODate object, but never found out how so far)
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CreateCourse {
pub tutor_id: ObjectId,
pub course_name: String,
pub course_description: Option<String>,
pub course_format: Option<String>,
pub course_structure: Option<String>,
pub course_type_id: String,
pub course_duration: Option<String>,
pub course_price_in_cents: Option<u32>,
pub course_level: Option<String>,
pub course_datetime: DateTime<Utc>,
}
but then I wrote a new handler to update a course, and I am using update_one with the doc! macro with $set for that operation, but when I commit to the database, it is changing the String field to a Datetime(ISODate) one.
let update_result = collection.update_one(
doc! {
"_id": &course_id,
},
doc! {
"$set": {
"course_name": new_course_record.course_name,
"course_description": new_course_record.course_description,
"course_format": new_course_record.course_format,
"course_structure": new_course_record.course_structure,
"course_type_id":new_course_record.course_type_id ,
"course_duration": new_course_record.course_duration,
"course_price_in_cents":new_course_record.course_price_in_cents ,
"course_level": new_course_record.course_level,
"course_datetime": new_course_record.course_datetime,
}
},
None,
).await?;
This is causing all kind of trouble since once a course is updated, the handler to get back the course details fails, since it was expecting a String and is now receiving a map instead(the Datetime object)
"error": "The server returned an invalid reply to a database operation: invalid type: map, expected a formatted date and time string or a unix timestamp"
What am I doing wrong here? Why is "update_one" writing it as a string, but the doc! macro writing the same information as a Datetime object? What should be done to make both behave the same way? Is there a way to make insert_one write the information as a Datetime object too?(that would be ideal)