Skip to content

Commit 629dfc1

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 289438b commit 629dfc1

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

+10
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,14 @@ CountOptions getCountOptions(@Nullable Class<?> domainType, @Nullable Consumer<C
567569
if (query.getSkip() > 0) {
568570
options.skip((int) query.getSkip());
569571
}
572+
if(query.getMeta().hasValues()) {
573+
if(query.getMeta().getMaxTimeMsec() != null && query.getMeta().getMaxTimeMsec() > 0) {
574+
options.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS);
575+
}
576+
if(StringUtils.hasText(query.getMeta().getComment())) {
577+
options.comment(query.getMeta().getComment());
578+
}
579+
}
570580

571581
HintFunction hintFunction = HintFunction.from(query.getHint());
572582

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

+20
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,26 @@ void stillUsesCountDocumentsForNonEmptyQueryEvenIfEstimationEnabled() {
24022402
verify(collection).countDocuments(any(Document.class), any());
24032403
}
24042404

2405+
@Test // GH-4374
2406+
void countConsidersMaxTimeMs() {
2407+
2408+
template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").maxTimeMsec(5000), Human.class);
2409+
2410+
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
2411+
verify(collection).countDocuments(any(Document.class), options.capture());
2412+
assertThat(options.getValue().getMaxTime(TimeUnit.MILLISECONDS)).isEqualTo(5000);
2413+
}
2414+
2415+
@Test // GH-4374
2416+
void countPassesOnComment() {
2417+
2418+
template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").comment("rocks!"), Human.class);
2419+
2420+
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
2421+
verify(collection).countDocuments(any(Document.class), options.capture());
2422+
assertThat(options.getValue().getComment()).isEqualTo(BsonUtils.simpleToBsonValue("rocks!"));
2423+
}
2424+
24052425
@Test // GH-3984
24062426
void templatePassesOnTimeSeriesOptionsWhenNoTypeGiven() {
24072427

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;
@@ -1532,6 +1533,26 @@ void stillUsesCountDocumentsForNonEmptyQueryEvenIfEstimationEnabled() {
15321533
verify(collection).countDocuments(any(Document.class), any());
15331534
}
15341535

1536+
@Test // GH-4374
1537+
void countConsidersMaxTimeMs() {
1538+
1539+
template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").maxTimeMsec(5000), Person.class).subscribe();
1540+
1541+
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
1542+
verify(collection).countDocuments(any(Document.class), options.capture());
1543+
assertThat(options.getValue().getMaxTime(TimeUnit.MILLISECONDS)).isEqualTo(5000);
1544+
}
1545+
1546+
@Test // GH-4374
1547+
void countPassesOnComment() {
1548+
1549+
template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").comment("rocks!"), Person.class).subscribe();
1550+
1551+
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
1552+
verify(collection).countDocuments(any(Document.class), options.capture());
1553+
assertThat(options.getValue().getComment()).isEqualTo(BsonUtils.simpleToBsonValue("rocks!"));
1554+
}
1555+
15351556
@Test // GH-2911
15361557
void insertErrorsOnPublisher() {
15371558

0 commit comments

Comments
 (0)