-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. | ||
* |
There was a problem hiding this comment.
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.
* | |
* | |
* When multiple documents match the query, the sort order specifies the document | |
* to be updated first. | |
* |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
I originally didn't include it because the ticket specifically dealt with |
/** | ||
* 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 | ||
*/ |
There was a problem hiding this comment.
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:
/** | |
* 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.
/** | ||
* 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 | ||
*/ |
There was a problem hiding this comment.
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?
/** | |
* 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.
There was a problem hiding this comment.
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.
public BsonDocument getSort() { | ||
return sort; | ||
} | ||
|
||
public MixedBulkWriteOperation sort(@Nullable final BsonDocument sort) { | ||
this.sort = sort; | ||
return this; | ||
} | ||
|
There was a problem hiding this comment.
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
.
Ticket
JAVA-5722
Description
This PR adds a sort option for
updateOne
andreplaceOne
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
./gradlew check
UnifiedCRUDTest.java