mongodb / mongo-rust-driver

The official MongoDB Rust Driver
https://www.mongodb.com/docs/drivers/rust/current/
Apache License 2.0
1.44k stars 164 forks source link

Positional $ operator gets the index wrong #953

Open moy2010 opened 1 year ago

moy2010 commented 1 year ago

Versions/Environment

  1. What version of Rust are you using? 1.70.0
  2. What operating system are you using? Ubuntu 22.04
  3. What versions of the driver and its dependencies are you using? mongodb 2.5.0 and bson 2.6.1
  4. What version of MongoDB are you using? 6.0.6
  5. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)? Standalone

Describe the bug

Using the $ positional operator in an update_one operation gets the index for the update wrong.

To Reproduce

Example of the query that shows the error:

my_collection
  .update_one(
    doc! { "my_field.type": "UpdateMe" },
    doc! { "$set": { "my_field.$.type": "Updated" } },
    None,
  )
.await?;

Let's assume that I'm running the integration tests and the my_field array only has one document. In this case, I'm expecting the $ operator to become the index 0, but unfortunately this is not the case and the example above will actually insert a new document into the array at index 1.

I validated that this is a bug by manually replacing the operator with the expected index, and then the integration tests pass:

my_collection
  .update_one(
    doc! { "my_field.type": "UpdateMe" },
    doc! { "$set": { "my_field.0.type": "Updated" } },
    None,
  )
.await?;
abr-egn commented 1 year ago

Hi!

At first glance, this doesn't look like behavior specific to the Rust driver. Can you check with the driver for a different language and see if this behavior is present there as well?