如何在“wither”Rust 中使用 String 而不是 ObjectId?

问题描述 投票:0回答:3

我这里有这个代码:

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

我想将“ObjectId”更改为字符串,这样我就可以使用Ulid

mongodb rust rust-cargo serde
3个回答
0
投票

如果您需要字符串使用

let str_id: String = account.id.unwrap().to_hex();  

0
投票

使用 MongoDB crate 检查一下它对我有用,但你可以尝试一下解决方案


0
投票

您可以尝试这里描述的解决方案: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,
}
© www.soinside.com 2019 - 2024. All rights reserved.