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 5 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
Comment on lines 225 to 232
/**
* Gets the sort criteria to apply to the query. The default is null, which means that the documents will be returned in an undefined
* order.
*
* @return a document describing the sort criteria
* @since 5.3
* @mongodb.driver.manual reference/method/db.collection.replaceOne/ Sort
*/
Copy link
Member

Choose a reason for hiding this comment

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

The sort criteria in the replace operation determines the order in which matching documents are processed. It does not return documents to the caller unless it’s used in a specific operation like findAndModify.

We should also specify the minimum server version @mongodb.server.release 8.0 for this option.

Should we clarify this in the Javadoc? Here’s a suggestion to make this more explicit:

Suggested change
/**
* Gets the sort criteria to apply to the query. The default is null, which means that the documents will be returned in an undefined
* order.
*
* @return a document describing the sort criteria
* @since 5.3
* @mongodb.driver.manual reference/method/db.collection.replaceOne/ Sort
*/
/**
* Gets the sort criteria to apply to the operation.
*
* <p>
* The sort criteria determines which document the operation replaces if the query matches multiple documents.
* The first document matched by the sort criteria will be replaced.
* The default is null, which means no specific sort criteria is applied.
*
* @return a document describing the sort criteria, or null if no sort is specified.
* @mongodb.driver.manual reference/method/db.collection.replaceOne/ Sort
* @mongodb.server.release 8.0
* @since 5.3
* @see #sort(Bson)
*/

If the ReplaceOptions.getSort() method Javadoc gets updated, we should make similar changes to the equivalent UpdateOptions method for consistency.

Comment on lines 238 to 246
/**
* Sets the sort criteria to apply to the query. When multiple documents match the query, the sort order specifies the document to be
* updated first.
*
* @param sort the sort criteria, which may be null.
* @return this
* @since 5.3
* @mongodb.driver.manual reference/method/db.collection.replaceOne/ Sort
*/
Copy link
Member

Choose a reason for hiding this comment

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

The term replaces might be more appropriate than updates here, as this operation replaces the entire document.

We should also specify the minimum server version that supports this option.

What do you think about updating the Javadoc to something like this?

Suggested change
/**
* Sets the sort criteria to apply to the query. When multiple documents match the query, the sort order specifies the document to be
* updated first.
*
* @param sort the sort criteria, which may be null.
* @return this
* @since 5.3
* @mongodb.driver.manual reference/method/db.collection.replaceOne/ Sort
*/
/**
* Sets the sort criteria to apply to the operation. A null value means no sort criteria is set.
*
* <p>
* The sort criteria determines which document the operation replaces if the query matches multiple documents.
* The first document matched by the specified sort criteria will be replaced.
*
* @param sort the sort criteria, which may be null.
* @return this
* @mongodb.driver.manual reference/method/db.collection.replaceOne/ Sort
* @mongodb.server.release 8.0
* @since 5.3
*/

If the ReplaceOptions.sort() method Javadoc gets updated, we should make similar changes to the equivalent UpdateOptions method for consistency.

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 also add unit tests to UpdateOptionsSpecification and UpdateRequestSpecification.

Additionally, we should update the tests in MongoCollectionSpecification to cover the logic for the new option.

Comment on lines 134 to 142
public BsonDocument getSort() {
return sort;
}

public MixedBulkWriteOperation sort(@Nullable final BsonDocument sort) {
this.sort = sort;
return this;
}

Copy link
Member

@vbabanin vbabanin Dec 27, 2024

Choose a reason for hiding this comment

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

These methods don’t appear to be used. Should we consider removing them?

This also applies to ReplaceRequest.

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