gpt4 book ai didi

mongodb - 在 MongoRepository 中使用限制和跳过

转载 作者:行者123 更新时间:2023-12-02 01:39:21 26 4
gpt4 key购买 nike

我们正在进行一个从 mongoDB 获取数据的项目。我们创建了如下的存储库类

@Repository
public interface CustomerRepository extends MongoRepository<Customer,String>{
List<Customer> customers = findByCustomerId(final String customerId);
}

我们希望添加跳过/偏移和限制参数以用作 findByCustomerId 方法的一部分。其中 limit 用于定义返回的记录数,skip/offset 定义我们需要获取记录的记录数。

请帮助我们如何使用 MongoRepository 以最佳方式实现这一点。

最佳答案

有两种方法可以做到这一点。

  1. 使用@Aggregation此答案中提到的注释。 https://stackoverflow.com/a/71292598/8470055

例如:

@Repository
public interface CustomerRepository extends MongoRepository<Customer,String>{

@Aggregation(pipeline = {
"{ '$match': { 'customerId' : ?0 } }",
"{ '$sort' : { 'customerId' : 1 } }",
"{ '$skip' : ?1 }",
"{ '$limit' : ?2 }"
})
List<Customer> findByCustomerId(final String customerId, int skip, int limit);

@Aggregation(pipeline = {
"{ '$match': { 'customerId' : ?0 } }",
"{ '$sort' : { 'customerId' : 1 } }",
"{ '$skip' : ?1 }"
})
Page<Customer> findCustomers(final String customerId, int skip, Pageable pageable);

}

$match运算符的查询可能需要修改,以便更好地反射(reflect)匹配文档需要满足的条件。

  1. 使用Pageable查询方法中的参数并提供 PageRequest来自调用 Repository 方法的层,如本答案所示。 https://stackoverflow.com/a/10077534/8470055

对于问题中的代码片段,这就变成了。

@Repository
public interface CustomerRepository extends MongoRepository<Customer,String> {

Page<Customer> findByCustomerId(final String customerId, Pageable pageable);

}

// -------------------------------------------------------
// Call the repository method from a service
@Service
public class CustomerService {

private final CustomerRepository customerRepository;

public CustomerService(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

public List<Customer> getCustomers(String customerId, int skip, int limit) {
// application-specific handling of skip and limit arguments
int page = 1; // calculated based on skip and limit values
int size = 5; // calculated based on skip and limit values
Page<Customer> page = customerRepository.findByCustomerId(customerId,
PageRequest.of(page, size, Sort.Direction.ASC, "customerId"));
List<Customer> customers = page.getContent();

/*
Here, the query method will retrieve 5 documents from the second
page.
It skips the first 5 documents in the first page with page index 0.
This approach requires calculating the page to retrieve based on
the application's definition of limit/skip.
*/
return Collections.unmodifiableList(customers);
}
}

聚合方法更有用。如果结果仅限于少数文档,则查询方法可以返回 List<Customer> .如果有很多文档,那么查询方法可以修改为使用Pageable。返回 Page<Customer> 的参数翻阅文档。

请参阅 Spring Data 和 MongoDB 文档。

https://docs.spring.io/spring-data/mongodb/docs/3.2.10/reference/html/#mongo.repositories

https://docs.spring.io/spring-data/mongodb/docs/3.2.10/reference/html/#mongodb.repositories.queries.aggregation

https://docs.spring.io/spring-data/mongodb/docs/3.2.10/api/org/springframework/data/mongodb/repository/Aggregation.html

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Pageable.html

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/PageRequest.html

MongoDB 聚合 - https://www.mongodb.com/docs/manual/meta/aggregation-quick-reference/

动态查询

自定义 Spring 数据存储库实现以及使用 MongoTemplate应该有助于实现动态查询。

自定义存储库 - https://docs.spring.io/spring-data/mongodb/docs/3.2.10/reference/html/#repositories.custom-implementations

MongoTemplate - https://docs.spring.io/spring-data/mongodb/docs/3.2.10/api/org/springframework/data/mongodb/core/MongoTemplate.html

关于mongodb - 在 MongoRepository<Customer,String> 中使用限制和跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71887036/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com