Skip to content

Positional $ operator gets the index wrong #953

Open
@moy2010

Description

@moy2010

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions