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

Serialization issue with Uuid #466

Closed
AzHicham opened this issue Mar 26, 2024 · 3 comments
Closed

Serialization issue with Uuid #466

AzHicham opened this issue Mar 26, 2024 · 3 comments

Comments

@AzHicham
Copy link

Versions/Environment

  1. What version of Rust are you using?
    1.77.0
  2. What operating system are you using?
    Ubuntu 23.04
  3. What versions of the driver and its dependencies are you using? (Run
    cargo pkgid mongodb & cargo pkgid bson)
    registry+https://github.com/rust-lang/crates.io-index#mongodb@2.8.2
    registry+https://github.com/rust-lang/crates.io-index#bson@2.9.0
  4. What version of MongoDB are you using? (Check with the MongoDB shell using db.version())
    db version v7.0.7
  5. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
    standalone

Describe the bug

I have a simple collection of this struct Analysis { _id: Uuid, value: f32 }
I insert analysis in my collection with this:
let res = collection.insert_one(&Analysis { _id: Uuid::new_v4(), value: 0_f32 }, None).await?; dbg!(res)

Then I try to find this analysis in my collection this way:

let filter = doc! {"_id": { "$eq": id } }; // id is a Uuid
let res = collection.find_one(filter, None).await?;

But without success...

I find out that the serialization involved during inserting will insert uuids as Binary::subtype::Generic but the serialization involved during the find will use Binary::subtype::Uuid

Making my find always return None ....

To reproduce:

let options = SerializerOptions::builder().human_readable(false).build();
dbg!(bson::to_bson_with_options(&Uuid::new_v4(), options));

Output:

[src/database/mongodb/analysis.rs:44:9] &filter = Document({
    "_id": Document({
        "$eq": Binary {
            subtype: Generic,
            bytes: [
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                1,
            ],
        },
    }),
})
@abr-egn
Copy link
Contributor

abr-egn commented Mar 26, 2024

Hi!

Given that you're constructing your UUID with new_v4, I'm guessing that you're using the uuid crate directly. Unfortunately, the Serialize implementation for uuid::Uuid doesn't produce valid BSON UUIDs, for that you'll need to use bson::Uuid.

Copy link

github-actions bot commented Apr 3, 2024

There has not been any recent activity on this ticket, so we are marking it as stale. If we do not hear anything further from you, this issue will be automatically closed in one week.

@github-actions github-actions bot added the Stale label Apr 3, 2024
Copy link

There has not been any recent activity on this ticket, so we are closing it. Thanks for reaching out and please feel free to file a new issue if you have further questions.

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

No branches or pull requests

2 participants