mongo-rust-driver icon indicating copy to clipboard operation
mongo-rust-driver copied to clipboard

Positional $ operator gets the index wrong

Open moy2010 opened this issue 1 year ago • 1 comments

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?;

moy2010 avatar Sep 07 '23 23:09 moy2010