mongo-rust-driver
mongo-rust-driver copied to clipboard
Positional $ operator gets the index wrong
Versions/Environment
- What version of Rust are you using? 1.70.0
- What operating system are you using? Ubuntu 22.04
- What versions of the driver and its dependencies are you using?
mongodb
2.5.0 andbson
2.6.1 - What version of MongoDB are you using? 6.0.6
- 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?;