Show how to use Limit with repository queries.

Add methods taking a Limit parameter to various samples.

Original pull request: #672
Closes #671
This commit is contained in:
Christoph Strobl
2023-11-02 08:49:24 +01:00
committed by Mark Paluch
parent e90be0c65b
commit f98c7b9c93
13 changed files with 167 additions and 0 deletions

View File

@@ -15,6 +15,9 @@
*/
package example.springdata.mongodb.people;
import java.util.List;
import org.springframework.data.domain.Limit;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -37,6 +40,17 @@ public interface ReactivePersonRepository extends ReactiveCrudRepository<Person,
*/
Flux<Person> findByLastname(String lastname);
/**
* Find at most the number of users defined via maxResults with the given lastname.
* This method will be translated into a query by constructing it directly from the method name as there is no other
* query declared.
*
* @param lastname
* @param maxResults the maximum number of results returned.
* @return
*/
Flux<Person> findByLastname(String lastname, Limit maxResults);
/**
* String query selecting one entity.
*

View File

@@ -18,6 +18,7 @@ package example.springdata.mongodb.people;
import static org.assertj.core.api.Assertions.*;
import example.springdata.mongodb.util.MongoContainers;
import org.springframework.data.domain.Limit;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -157,6 +158,14 @@ class ReactivePersonRepositoryIntegrationTest {
repository.findByLastname("White").as(StepVerifier::create).expectNextCount(2).verifyComplete();
}
/**
* Limit result size.
*/
@Test
void shouldLimitResultSize() {
repository.findByLastname("White", Limit.of(1)).as(StepVerifier::create).expectNextCount(1).verifyComplete();
}
/**
* Fetch data using a string query.
*/