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

Allow updateOne and replaceOne to supply sort option #1585

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

joykim1005
Copy link
Contributor

@joykim1005 joykim1005 commented Dec 18, 2024

Ticket

JAVA-5722

Description

This PR adds a sort option for updateOne and replaceOne commands so if it matches more than one candidate document, the first one matched by sort order will be updated. It also syncs CRUD unified spec tests to this.

Testing

  • ran ./gradlew check
  • local testing - ran UnifiedCRUDTest.java

@joykim1005 joykim1005 marked this pull request as ready for review December 18, 2024 19:24
@joykim1005 joykim1005 requested review from a team and jyemin and removed request for a team December 18, 2024 19:25
@jyemin jyemin requested review from vbabanin and removed request for vbabanin December 18, 2024 19:26
@vbabanin vbabanin self-requested a review December 18, 2024 20:48
Copy link
Member

@vbabanin vbabanin left a comment

Choose a reason for hiding this comment

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

Looking good overall! I noticed in the spec changes here: https://github.com/mongodb/specifications/pull/1644/files#diff-d6a7b61abed64b14a2e1eb4827556e8ed74c84bc8ae1f635ff8de67a4cfbcfba that ReplaceOneOptions might also need to be updated. Should we include that in this PR?

@@ -40,6 +41,7 @@ public class UpdateOptions {
private String hintString;
private BsonValue comment;
private Bson variables;
private BsonDocument sort;
Copy link
Member

Choose a reason for hiding this comment

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

Let’s use the Bson type here to allow users to choose between Document and BsonDocument, similar to how it’s done in FindOneAndDeleteOptions and other cases.

*
* @return a document describing the sort criteria
* @since 5.3
* @mongodb.driver.manual reference/method/cursor.sort/ Sort
Copy link
Member

Choose a reason for hiding this comment

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

Should we remove the manual reference here and from the getSort method, given that it seems more relevant to the cursor?

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 referenced the findOptions and the other findOneAnd..Options and thought it might be helpful to have a reference! I don't have a strong opinion here though.

* @since 5.3
* @mongodb.driver.manual reference/method/cursor.sort/ Sort
*/
public UpdateOptions sort(@Nullable final BsonDocument sort) {
Copy link
Member

@vbabanin vbabanin Dec 19, 2024

Choose a reason for hiding this comment

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

Should we consider adding a note in the Javadoc to clarify that sort is not applicable to updateMany? This could help avoid confusion for users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good point, will go ahead and add it!


/**
* Sets the sort criteria to apply to the query.
*
Copy link
Member

Choose a reason for hiding this comment

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

Could we clarify the purpose of this sort option in the Javadoc? It would be helpful to explain how it determines which document is updated when multiple matches are found.

Suggested change
*
*
* When multiple documents match the query, the sort order specifies the document
* to be updated first.
*

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do!

@@ -31,6 +32,7 @@ public final class BulkWriteOptions {
private Boolean bypassDocumentValidation;
private BsonValue comment;
private Bson variables;
private BsonDocument sort;
Copy link
Member

Choose a reason for hiding this comment

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

Judging by the spec, BulkWriteOptions does not seem to require a sort option.

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 overlooked that part, will remove it!

@joykim1005
Copy link
Contributor Author

Looking good overall! I noticed in the spec changes here: https://github.com/mongodb/specifications/pull/1644/files#diff-d6a7b61abed64b14a2e1eb4827556e8ed74c84bc8ae1f635ff8de67a4cfbcfba that ReplaceOneOptions might also need to be updated. Should we include that in this PR?

I originally didn't include it because the ticket specifically dealt with updateOne, but I can go ahead and include that in this PR and update the ticket as it is adding the same option!

@joykim1005 joykim1005 requested a review from vbabanin December 19, 2024 21:59
@joykim1005 joykim1005 changed the title Allow update to supply sort option Allow updateOne and replaceOne to supply sort option Dec 26, 2024
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.

2 participants