Open wbrickner opened 3 years ago
Hey there. So, take a look at the bson docs here: https://docs.rs/bson/1.2.0/bson/ser/fn.to_bson.html
Though there are a few ways to deal with this, using this function to serialize your type as Bson is probably best, as it will ensure that if you actually have some BSON incompatible serde rules on your enum, this function will catch such issues (as an error).
That is generally what I would recommend in such cases, as this is the same mechanism which would be used if you have that enum type embedded in a struct which you are serializing/deserializing as bson.
So I would instead do something like
"$set": doc! {
"standing": to_bson(&UserStanding::Active).unwrap()
}
Is that correct?
In another case, imagine I have
struct User {
pub my_field: Option<MyWeirdType>
}
How can I find all documents which have a Some variant without explicitly constructing the thing I'm looking for? Do I need to step down to the serde/BSON serialization level and explicitly search for something like:
{ "my_field": {"tag": "Some" } }
This feels ugly and error-prone, I hope there is a better way to do this.
@wbrickner so, at the end of the day the enum is just another field in the DB. So as long as your enum field in indexed in the DB (otherwise it may be dead slow and may negatively impact your DB), then you have to treat it like any other field you are filtering on.
So if you know that your enum value serializes as {"tag": "Some"}
, then you could manually write that, or you could use to_bson(...)
to serialize its representation first and use that as the query. If you don't like those options, you could create your own as_bson
method (or whatever you want to call it) which will just return the Bson enum representation for you as static/hard-coded values.
Thoughts?
Hello ππ»
I am trying to update a document.
I need to set the field
standing
, of typeUserStanding
:When I try to do this:
I get the error
Thank you.