Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query by enum type #133

Open
sayanb opened this issue Sep 24, 2023 · 1 comment
Open

Query by enum type #133

sayanb opened this issue Sep 24, 2023 · 1 comment

Comments

@sayanb
Copy link

sayanb commented Sep 24, 2023

I am storing records and one of the fields is an Enum type

pub enum Cuisine {
    French,
    Chinese,
    Thai,
    Japanese,
    Korean,
    Indian,
    Italian,
    Mexian,
    Turkish,
    Other(String),
}

There is no problem with the insertion. However, a query

let restaurants = collection.find(doc! {
        "cuisine": cuisine,
 })?;

where cuisine is a variant of Cuisine type

produces the following compiler error:

error[E0277]: the trait bound `Bson: From<Cuisine>` is not satisfied
the trait `From<Cuisine>` is not implemented for `Bson`

Is there a way to query by an Enum variant at all, or this is currently not possible without implementing Display trait for it, then converting it to a String?

@vincentdchan
Copy link
Collaborator

vincentdchan commented Mar 1, 2024

You can check the bson spec. Looks like there is a possible way is to convert enum to number or string, it depends on you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants