mongo-rust-driver
mongo-rust-driver copied to clipboard
RUST-2054 Incorrect default index name when creating an index
Versions/Environment
- What version of Rust are you using?
rustc 1.81.0 (eeb90cda1 2024-09-04) - What operating system are you using?
Ubuntu 22.04.4 LTS on WSL/Windows 10 Pro 22H2 - What versions of the driver and its dependencies are you using?
[email protected]and[email protected] - What version of MongoDB are you using?
MongoDB 8.0.1 Community - What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
standalone
Describe the bug
When creating indexes with the text, 2d, 2dsphere or hashed types the default index name generated by the driver does not follow the documentation.
Steps to reproduce
- Start a local MongoDB server
- Run this snippet
use mongodb::bson::{doc, Document};
use mongodb::error::Result;
use mongodb::{Client, IndexModel};
#[tokio::main]
async fn main() -> Result<()> {
let client = Client::with_uri_str("mongodb://localhost:27017").await?;
let db = client.database("test");
db.create_collection("test").await?;
let col = db.collection::<Document>("test");
col.create_index(IndexModel::builder().keys(doc! { "field": "2d" }).build()).await?;
Ok(())
}
- Verify the index name with the shell or Compass
Expected result
The index is named field_2d.
Actual result
The index is named field_"2d".
The problem boils down to https://github.com/mongodb/mongo-rust-driver/blob/ef0893f77627a87b9eff7fbf4fd2059ea6b19211/src/index.rs#L38
where the Bson conversion of v to a string is always wrapped by ".
Since this logic is not public I had to reproduce it in my code and that's how I found the bug.
Thanks for the detailed report! I'm looking into this now.
I just merged https://github.com/mongodb/mongo-rust-driver/pull/1226 which fixes this, and will go out in our next release. Thanks again for the report!