Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRIVERS-2822 Add sort option to updateOne #1644

Merged
merged 12 commits into from
Oct 7, 2024

Conversation

qingyang-hu
Copy link
Contributor

@qingyang-hu qingyang-hu commented Aug 30, 2024

Summary

Add a sort option to updateOne() so if it matches more than one candidate document the first one matched by the sort order will be updated.

Background & Motivation

The new syntax allows a sort option to be supplied to an update command. The upstream server changes are at https://jira.mongodb.org/browse/SERVER-78140.

Some example commands can be found at: mongodb/mongo@e443421#diff-c9ff788fbdeb3594c45d85c449a%5B%E2%80%A6%5D21f5ef32ede8f9c858e6e3072e0c8R27

The corresponding mongosh docs:
https://www.mongodb.com/docs/upcoming/reference/method/db.collection.updateOne/
Though the command docs have not been updated yet.


Please complete the following before merging:

  • Update changelog.
  • Make sure there are generated JSON files from the YAML test files.
  • Test changes in at least one language driver.
  • Test these changes against all server versions and topologies (including standalone, replica set, sharded
    clusters, and serverless).

@qingyang-hu qingyang-hu requested a review from a team as a code owner August 30, 2024 20:29
@qingyang-hu qingyang-hu requested review from jmikola and blink1073 and removed request for a team August 30, 2024 20:29
source/crud/crud.md Outdated Show resolved Hide resolved
source/crud/crud.md Outdated Show resolved Hide resolved
source/crud/tests/unified/bulkWrite-updateOne-sort.yml Outdated Show resolved Hide resolved
source/crud/tests/unified/bulkWrite-updateOne-sort.yml Outdated Show resolved Hide resolved
source/crud/tests/unified/updateMany-sort.yml Outdated Show resolved Hide resolved
source/crud/tests/unified/updateMany-sort.yml Outdated Show resolved Hide resolved
source/crud/tests/unified/updateMany-sort.yml Outdated Show resolved Hide resolved
source/crud/tests/unified/updateOne-sort.yml Show resolved Hide resolved
source/crud/tests/unified/updateOne-sort.yml Show resolved Hide resolved
object: collection0
arguments:
filter: { _id: { $gt: 1 } }
sort: { _id: -1 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is a bit odd for drivers like pymongo that don't share options between update_one and update_many. Could we add a comment here that explains that this test should be skipped for such drivers? I would not want implementors to accidentally add a non-functional sort option just to pass this test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be OK with removing this test entirely since it's not testing client-side behavior at all.

We should be able to trust that the server is testing that multi: true and sort conflict.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove this test if the Go driver is the only one who shares the UpdateOptions for both update_one and update_many. I will also separate UpdateOneOptions and UpdateManyOptions because the Go driver will introduce a new options interface anyway in v2.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of duplicating UpdateOneOptions and UpdateManyOptions. I think it was enough to have UpdateOptions with the note that sort does not work with UpdateMany but I'll defer to others review.

Copy link
Member

@jmikola jmikola Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the interest of pragmatism, I'm completely OK with keeping the singular UpdateOptions and leaving a note about sort not applying to updateMany.

@qingyang-hu: Also, I think this was previously overlooked in one of my previous comments, but shouldn't this PR entail a change to Write Models in the bulk write spec? That has a separate model classes for UpdateOne and ReplaceOne, so sort can just go where (no concern for a common options interface as we have in the CRUD spec).

Note that the bulk write spec also has separate tests (client-bulkWrite- prefix), so I don't think the existing bulkWrite-*yml tests in this PR are sufficient.

@ShaneHarvey ShaneHarvey removed their request for review September 17, 2024 01:31
@ShaneHarvey
Copy link
Member

Removing myself, I'll defer to Jeremy + the bulk write spec authors.

@jmikola
Copy link
Member

jmikola commented Sep 17, 2024

@qingyang-hu: Please ping me for another review once the Bulk Write spec/tests are updated.

@qingyang-hu
Copy link
Contributor Author

@jmikola, can you please take a look again? I've added the sort to the client-level bulk write.

Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions to use YAML anchors/aliases for consistency, and a question about doc comments in the spec.

source/crud/bulk-write.md Show resolved Hide resolved
source/crud/tests/unified/bulkWrite-replaceOne-sort.yml Outdated Show resolved Hide resolved
@qingyang-hu
Copy link
Contributor Author

@jmikola, can you please review the updates?

- { _id: 3, x: 33 }

_yamlAnchors:
namespace: &namespace "crud-tests.coll0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for using the convention from Top-level Fields here.

@qingyang-hu qingyang-hu merged commit da8e120 into mongodb:master Oct 7, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants