2 / 2
Aug 2023

Versions/Environment

  1. rustc 1.71.1 (eb26296b5 2023-08-03)
  2. Windows 11 x64
[dependencies] mongodb = { version = "2.6.1", features = ["bson-uuid-1"] } uuid = "1.4.1"
  1. MongoDB v3.7
  2. Standalone

Description

I’ve encountered an issue where a MongoDB search using a UUID as a filter is not returning any results. This is unexpected, as I have documents in the collection with the exact UUID value I’m filtering by.

Here’s the code snippet in question:

#[tokio::main] async fn main() -> mongodb::error::Result<()> { // Connect to MongoDB. let client = Client::with_uri_str("mongodb://localhost:27017").await.unwrap(); let database = client.database("your_database_name"); let collection = database.collection("your_collection_name"); // UUID you want to search for. let desired_uuid = Uuid::parse_str("936DA01F-9ABD-4D9D-80C7-02AF85C822A8").unwrap(); let filter = bson::doc! { "uuid": desired_uuid }; // Fetch documents that match the filter criteria. let mut cursor = collection.find(Some(filter), None).await.unwrap(); // Iterate over the results of the cursor. while let Some(result) = cursor.try_next().await.unwrap() { println!("result: {:?}", result); }; Ok(()) }

Despite the above code, no results are printed to the console. I’ve double-checked my collection, and I’m certain that there are documents with the “uuid” field in Binary format that matches the desired_uuid.

Is there any known issue regarding filtering with UUID?

For anybody looking for solution please find my GH issue here.

What is the actual solution?
Remember to use bson::Uuid when inserting to your database instead of uuid:Uuid. It is recommended to use bson::Uuid when using UUIDs within you project with mongodb rust driver.