Skip to content

Support hints on Update. #4311

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>4.1.x-GH-3218-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>4.1.x-GH-3218-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>4.1.x-GH-3218-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>4.1.x-GH-3218-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Function object to apply a query hint. Can be an index name or a BSON document.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 4.1
*/
class HintFunction {
Expand Down Expand Up @@ -67,6 +68,23 @@ public boolean isPresent() {
return (hint instanceof String hintString && StringUtils.hasText(hintString)) || hint instanceof Bson;
}

/**
* Apply the hint to consumers depending on the hint format if {@link #isPresent() present}.
*
* @param registryProvider
* @param stringConsumer
* @param bsonConsumer
* @param <R>
*/
public <R> void ifPresent(@Nullable CodecRegistryProvider registryProvider, Function<String, R> stringConsumer,
Function<Bson, R> bsonConsumer) {

if (!isPresent()) {
return;
}
apply(registryProvider, stringConsumer, bsonConsumer);
}

/**
* Apply the hint to consumers depending on the hint format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ UpdateOptions getUpdateOptions(@Nullable Class<?> domainType, @Nullable Consumer
.arrayFilters(update.getArrayFilters().stream().map(ArrayFilter::asDocument).collect(Collectors.toList()));
}

HintFunction.from(getQuery().getHint()).ifPresent(codecRegistryProvider, options::hintString, options::hint);
applyCollation(domainType, options::collation);

if (callback != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,28 @@ void updateManyShouldUseCollationWhenPresent() {
assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}

@Test // GH-3218
void updateUsesHintStringFromQuery() {

template.updateFirst(new Query().withHint("index-1"), new Update().set("spring", "data"), Human.class);

ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
verify(collection).updateOne(any(Bson.class), any(Bson.class), options.capture());

assertThat(options.getValue().getHintString()).isEqualTo("index-1");
}

@Test // GH-3218
void updateUsesHintDocumentFromQuery() {

template.updateFirst(new Query().withHint("{ name : 1 }"), new Update().set("spring", "data"), Human.class);

ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
verify(collection).updateOne(any(Bson.class), any(Bson.class), options.capture());

assertThat(options.getValue().getHint()).isEqualTo(new Document("name", 1));
}

@Test // DATAMONGO-1518
void replaceOneShouldUseCollationWhenPresent() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,28 @@ void updateManyShouldUseCollationWhenPresent() {
verify(collection).updateMany(any(), any(Bson.class), options.capture());

assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}

@Test // GH-3218
void updateUsesHintStringFromQuery() {

template.updateFirst(new Query().withHint("index-1"), new Update().set("spring", "data"), Person.class).subscribe();

ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
verify(collection).updateOne(any(Bson.class), any(Bson.class), options.capture());

assertThat(options.getValue().getHintString()).isEqualTo("index-1");
}

@Test // GH-3218
void updateUsesHintDocumentFromQuery() {

template.updateFirst(new Query().withHint("{ firstname : 1 }"), new Update().set("spring", "data"), Person.class).subscribe();

ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
verify(collection).updateOne(any(Bson.class), any(Bson.class), options.capture());

assertThat(options.getValue().getHint()).isEqualTo(new Document("firstname", 1));
}

@Test // DATAMONGO-1518
Expand Down
1 change: 1 addition & 0 deletions src/main/asciidoc/reference/mongodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ Most methods return the `Update` object to provide a fluent style for the API.
* *updateMulti*: Updates all objects that match the query document criteria with the updated document.

WARNING: `updateFirst` does not support ordering. Please use <<mongo-template.find-and-upsert, findAndModify>> to apply `Sort`.
NOTE: Index hints for the update operation can be provided via `Query.withHint(...)`.

[[mongodb-template-update.update]]
==== Methods in the `Update` Class
Expand Down