Spring Mongo 存储库:通过传递 ids 列表来获取对象列表

问题描述 投票:0回答:1

我正在尝试添加 mongo 存储库查询,它将获取提供参数的所有对象作为 customerId 列表

public class Customer {
  @Id
  private String id;
  private String name;
  private String customerId;
}

我存储的数据是

[
{
    "customerId": "customer1",
    "id": "001",
    "name": "testName1",
    "surname": "testSurname1"
},
{
    "customerId": "customer2",
    "id": "002",
    "name": "testName2",
    "surname": "testSurname2"
},
{
    "customerId": "customer3",
    "id": "003",
    "name": "testName3",
    "surname": "testSurname3"
}

]

现在我需要通过传递 customerId 列表来检索客户对象

public interface CustomerRepository extends MongoRepository<Task, String> {
   List<Customer> findAllByCustomerId(List.of("customer1","customer2","customer4"));
}

期望它应该返回客户 1 和客户 2 对象。 谢谢。

spring spring-boot mongodb-query spring-data-mongodb
1个回答
0
投票

在MongoRepository Generig中,第一个参数应该是预期的实体类型。试试这个:

public interface CustomerRepository extends MongoRepository<Customer, String> {
   List<Customer> findAllByCustomerIdIn(List<String> customerIds);
}
© www.soinside.com 2019 - 2024. All rights reserved.