在 MongoRepository 中使用 Skip 和 Limit

1、概览

Spring Data MongoDBMongoRepository 接口提供了一种简单的方式与 MongoDB Collection 进行交互。

本文将带你了解如何在 MongoRepository 中使用 limitskip

2、初始设置

首先,创建一个名为 StudentRepository 的 Repository,用于存储 Student 信息:

public interface StudentRepository extends MongoRepository<Student, String> {}

然后,向该 Repository 添加一些 Student 示例数据:

@Before
public void setUp() {
    Student student1 = new Student("A", "Abraham", 15L);
    Student student2 = new Student("B", "Usman", 30L);
    Student student3 = new Student("C", "David", 20L);
    Student student4 = new Student("D", "Tina", 45L);
    Student student5 = new Student("E", "Maria", 33L);

    studentList = Arrays.asList(student1, student2, student3, student4, student5);
    studentRepository.saveAll(studentList);
}

3、使用聚合管道(Aggregation Pipeline)

聚合管道是处理、转换和分析数据的强大工具。它的工作原理是将多个阶段(stage)连在一起,每个阶段执行特定的操作。这些操作包括过滤、分组、排序、分页等。

通过一个基本示例来使用 limitskip

@Test
public void whenRetrievingAllStudents_thenReturnsCorrectNumberOfRecords() {
    // WHEN
    List<Student> result = studentRepository.findAll(0L, 5L);
    // THEN
    assertEquals(5, result.size());
}

上述聚合管道会跳过指定数量的文档,并将限制输出文档的数量。

@Test
public void whenLimitingAndSkipping_thenReturnsLimitedStudents() {
    // WHEN
    List<Student> result = studentRepository.findAll(3L, 2L);
    // THEN
    assertEquals(2, result.size());
    assertEquals("Tina", result.get(0).getName());
    assertEquals("Maria", result.get(1).getName());
}

我们甚至还可以在复杂的聚合管道中应用 limitskip 功能:

@Aggregation(pipeline = {
  "{ '$match': { 'id' : ?0 } }",
  "{ '$sort' : { 'id' : 1 } }",
  "{ '$skip' : ?1 }",
  "{ '$limit' : ?2 }"
})
List<Student> findByStudentId(final String studentId, Long skip, Long limit);

测试如下:

@Test
public void whenFilteringById_thenReturnsStudentsMatchingCriteria() {
    // WHEN
    List<Student> result = studentRepository.findByStudentId("A", 0L, 5L);
    // THEN
    assertEquals(1, result.size());
    assertEquals("Abraham", result.get(0).getName());
}

4、使用 Pageable

在 Spring Data 中,Pageable 是一个接口,用于表示以分页方式检索数据的请求。从 MongoDB Collection 中查询数据时,Pageable 对象允许我们指定页码、每页数据量大小和排序等参数。这对于在应用中显示大型数据集特别有用,因为在这种情况下一次性显示所有项目会很慢。

来看看如何使用 Pageable 定义 Repository 方法实现高效的数据检索:

Page<Student> findAll(Pageable pageable);

添加一个测试:

@Test
public void whenFindByStudentIdUsingPageable_thenReturnsPageOfStudents() {
    // GIVEN
    Sort sort = Sort.by(Sort.Direction.DESC, "id");
    Pageable pageable = PageRequest.of(0, 5, sort);

    // WHEN
    Page<Student> resultPage = studentRepository.findAll(pageable);

    // THEN
    assertEquals(5, resultPage.getTotalElements());
    assertEquals("Maria", resultPage.getContent().get(0).getName());
}

5、总结

本文介绍了如何在 MongoRepository 中使用 skiplimit,此外,以及如何使用 Pageable 参数简化分页查询。


Ref:https://www.baeldung.com/spring-data-mongorepository-limit-skip