Skip to content

Commit ec55e28

Browse files
christophstroblmp911de
authored andcommitted
Fix missing query options when calling MongoOperations#count.
This commit makes sure to forward maxTimeMsec and comment options from the query to the CountOptions. Closes: #4374 Original pull request: #4378
1 parent b9af920 commit ec55e28

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Optional;
2222
import java.util.Set;
2323
import java.util.concurrent.ConcurrentHashMap;
24+
import java.util.concurrent.TimeUnit;
2425
import java.util.function.Consumer;
2526
import java.util.function.Function;
2627
import java.util.stream.Collectors;
@@ -66,6 +67,7 @@
6667
import com.mongodb.client.model.DeleteOptions;
6768
import com.mongodb.client.model.ReplaceOptions;
6869
import com.mongodb.client.model.UpdateOptions;
70+
import org.springframework.util.StringUtils;
6971

7072
/**
7173
* {@link QueryOperations} centralizes common operations required before an operation is actually ready to be executed.
@@ -567,6 +569,16 @@ CountOptions getCountOptions(@Nullable Class<?> domainType, @Nullable Consumer<C
567569
if (query.getSkip() > 0) {
568570
options.skip((int) query.getSkip());
569571
}
572+
573+
if(query.getMeta().hasValues()) {
574+
if(query.getMeta().getMaxTimeMsec() != null && query.getMeta().getMaxTimeMsec() > 0) {
575+
options.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
576+
}
577+
if(StringUtils.hasText(query.getMeta().getComment())) {
578+
options.comment(query.getMeta().getComment());
579+
}
580+
}
581+
570582
if (StringUtils.hasText(query.getHint())) {
571583

572584
String hint = query.getHint();

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,26 @@ void stillUsesCountDocumentsForNonEmptyQueryEvenIfEstimationEnabled() {
23072307
verify(collection).countDocuments(any(Document.class), any());
23082308
}
23092309

2310+
@Test // GH-4374
2311+
void countConsidersMaxTimeMs() {
2312+
2313+
template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").maxTimeMsec(5000), Human.class);
2314+
2315+
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
2316+
verify(collection).countDocuments(any(Document.class), options.capture());
2317+
assertThat(options.getValue().getMaxTime(TimeUnit.MILLISECONDS)).isEqualTo(5000);
2318+
}
2319+
2320+
@Test // GH-4374
2321+
void countPassesOnComment() {
2322+
2323+
template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").comment("rocks!"), Human.class);
2324+
2325+
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
2326+
verify(collection).countDocuments(any(Document.class), options.capture());
2327+
assertThat(options.getValue().getComment()).isEqualTo(BsonUtils.simpleToBsonValue("rocks!"));
2328+
}
2329+
23102330
@Test // GH-3984
23112331
void templatePassesOnTimeSeriesOptionsWhenNoTypeGiven() {
23122332

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import lombok.AllArgsConstructor;
2424
import lombok.Data;
2525
import lombok.NoArgsConstructor;
26+
import org.springframework.data.mongodb.util.BsonUtils;
2627
import reactor.core.publisher.Flux;
2728
import reactor.core.publisher.Mono;
2829
import reactor.test.StepVerifier;
@@ -1456,6 +1457,26 @@ void stillUsesCountDocumentsForNonEmptyQueryEvenIfEstimationEnabled() {
14561457
verify(collection).countDocuments(any(Document.class), any());
14571458
}
14581459

1460+
@Test // GH-4374
1461+
void countConsidersMaxTimeMs() {
1462+
1463+
template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").maxTimeMsec(5000), Person.class).subscribe();
1464+
1465+
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
1466+
verify(collection).countDocuments(any(Document.class), options.capture());
1467+
assertThat(options.getValue().getMaxTime(TimeUnit.MILLISECONDS)).isEqualTo(5000);
1468+
}
1469+
1470+
@Test // GH-4374
1471+
void countPassesOnComment() {
1472+
1473+
template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").comment("rocks!"), Person.class).subscribe();
1474+
1475+
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
1476+
verify(collection).countDocuments(any(Document.class), options.capture());
1477+
assertThat(options.getValue().getComment()).isEqualTo(BsonUtils.simpleToBsonValue("rocks!"));
1478+
}
1479+
14591480
@Test // GH-2911
14601481
void insertErrorsOnPublisher() {
14611482

0 commit comments

Comments
 (0)