Search code examples
mongodbrustrust-cargoserde

How to Use String instead of ObjectId in "wither" Rust?


I have this code here:

pub struct Account {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    pub email: String,
}

And I want to change "ObjectId" to String so I can use Ulid.


Solution

  • You can try this solution, described here: https://docs.rs/bson/latest/bson/oid/struct.ObjectId.html

    pub struct Account {
        #[serde(serialize_with = "bson::serde_helpers::serialize_object_id_as_hex_string")
        pub id: Option<ObjectId>,
        pub email: String,
    }