我希望能够将字段写入 mongo 文档,但在将对象作为响应传递给客户端时避免对其进行序列化。
我尝试使用#[serde(skip_serializing)]
. 但是,它并没有按我的预期工作,我认为这是因为当它从 mongo 库转换Region
使用时to_document
,它会使用 skip 属性进行序列化,因为它还使用 serde 序列化程序。如果我在这里错了,请纠正我。
我有两个定义如下的结构:
#[serde(rename_all = "camelCase")]
#[derive(Serialize, Deserialize, Debug)]
pub struct Region {
#[serde(alias = "_id")]
pub id: Option<i32>,
pub name: String,
...
pub areas: Vec<Area>,
...
}
#[serde(rename_all = "camelCase")]
#[derive(Serialize, Deserialize, Debug)]
pub struct Area {
pub id: Option<String>,
pub name: String,
#[serde(skip_serializing)]
pub ext_api_id: Option<String>,
}
在插入后手动检查文档时,没有extApiId
字段。
我尝试通过键(类似于document.insert("_id", id)
)手动将其添加到生成的文档中,但我找不到也无法弄清楚如何通过键字符串访问数组中的字段。理想情况下,无论如何我都不想这样做。
这是我的更新函数中的一些示例代码:
pub async fn update(&self, id: i32, mut region: Region) -> Result<i32, Box<dyn Error>> {
for mut area in &mut region.areas {
area.id = Some(get_slug_from_name(area.name.clone()));
}
let mut document = to_document(®ion)?;
document.remove("id");
document.insert("_id", id);
self.collection
.update_one(
doc! { "_id": id },
document,
UpdateOptions::builder().upsert(Some(true)).build(),
)
.await?;
Ok(id)
}
有关如何解决此问题的任何建议?我考虑过是否有Area
fieldInsertableArea
属性,我想我也需要一个InsertableRegion
for。