diff --git a/pom.xml b/pom.xml index 34654b778d..365d6ee0c4 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 4.1.0-SNAPSHOT + 4.1.x-4374-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml index 1b2a1390e6..72ff18f659 100644 --- a/spring-data-mongodb-benchmarks/pom.xml +++ b/spring-data-mongodb-benchmarks/pom.xml @@ -7,7 +7,7 @@ org.springframework.data spring-data-mongodb-parent - 4.1.0-SNAPSHOT + 4.1.x-4374-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 888aaff6f5..10c0977ad6 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -15,7 +15,7 @@ org.springframework.data spring-data-mongodb-parent - 4.1.0-SNAPSHOT + 4.1.x-4374-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 597ca94f38..41887915c9 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 4.1.0-SNAPSHOT + 4.1.x-4374-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java index fec9a12db3..dd62de7155 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java @@ -21,6 +21,7 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Collectors; @@ -66,6 +67,7 @@ import com.mongodb.client.model.DeleteOptions; import com.mongodb.client.model.ReplaceOptions; import com.mongodb.client.model.UpdateOptions; +import org.springframework.util.StringUtils; /** * {@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 0) { options.skip((int) query.getSkip()); } + if(query.getMeta().hasValues()) { + if(query.getMeta().getMaxTimeMsec() != null && query.getMeta().getMaxTimeMsec() > 0) { + options.maxTime(query.getMeta().getMaxTimeMsec(), TimeUnit.MILLISECONDS); + } + if(StringUtils.hasText(query.getMeta().getComment())) { + options.comment(query.getMeta().getComment()); + } + } HintFunction hintFunction = HintFunction.from(query.getHint()); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java index 056efc971b..1d59e7438a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java @@ -2402,6 +2402,26 @@ void stillUsesCountDocumentsForNonEmptyQueryEvenIfEstimationEnabled() { verify(collection).countDocuments(any(Document.class), any()); } + @Test // GH-4374 + void countConsidersMaxTimeMs() { + + template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").maxTimeMsec(5000), Human.class); + + ArgumentCaptor options = ArgumentCaptor.forClass(CountOptions.class); + verify(collection).countDocuments(any(Document.class), options.capture()); + assertThat(options.getValue().getMaxTime(TimeUnit.MILLISECONDS)).isEqualTo(5000); + } + + @Test // GH-4374 + void countPassesOnComment() { + + template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").comment("rocks!"), Human.class); + + ArgumentCaptor options = ArgumentCaptor.forClass(CountOptions.class); + verify(collection).countDocuments(any(Document.class), options.capture()); + assertThat(options.getValue().getComment()).isEqualTo(BsonUtils.simpleToBsonValue("rocks!")); + } + @Test // GH-3984 void templatePassesOnTimeSeriesOptionsWhenNoTypeGiven() { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java index 48b48a2e23..aaa0ba14e3 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java @@ -23,6 +23,7 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import org.springframework.data.mongodb.util.BsonUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -1532,6 +1533,26 @@ void stillUsesCountDocumentsForNonEmptyQueryEvenIfEstimationEnabled() { verify(collection).countDocuments(any(Document.class), any()); } + @Test // GH-4374 + void countConsidersMaxTimeMs() { + + template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").maxTimeMsec(5000), Person.class).subscribe(); + + ArgumentCaptor options = ArgumentCaptor.forClass(CountOptions.class); + verify(collection).countDocuments(any(Document.class), options.capture()); + assertThat(options.getValue().getMaxTime(TimeUnit.MILLISECONDS)).isEqualTo(5000); + } + + @Test // GH-4374 + void countPassesOnComment() { + + template.count(new BasicQuery("{ 'spring' : 'data-mongodb' }").comment("rocks!"), Person.class).subscribe(); + + ArgumentCaptor options = ArgumentCaptor.forClass(CountOptions.class); + verify(collection).countDocuments(any(Document.class), options.capture()); + assertThat(options.getValue().getComment()).isEqualTo(BsonUtils.simpleToBsonValue("rocks!")); + } + @Test // GH-2911 void insertErrorsOnPublisher() {