I want to be able to write a field into a mongo document, but avoid serializing it when passing the object as a response to the client.
I tried using #[serde(skip_serializing)]
.
However, it doesn't work as I intended, which I think is because when it converts Region
using to_document
from the mongo library, it gets serialized with the skip attribute, as it also uses the serde serializer. Please correct me if I'm wrong here.
I have two structs defined like the following:
#[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>,
}
When manually inspecting a document after an insert, there's no extApiId
field.
I tried manually adding it to the generated document by key (something like document.insert("_id", id)
), but I couldn't find nor figure out how to access fields within an array via key strings. Ideally, I'd like to not have to do this anyway.
Here's some example code from my update function:
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)
}
Any suggestions on how to work around this issue? I thought about having Area
and InsertableArea
without the field attribute, which I suppose I'd also need a InsertableRegion
for.