代码之家  ›  专栏  ›  技术社区  ›  sree

Spring Mongo仓库:通过传递id列表来获取对象列表

  •  0
  • sree  · 技术社区  · 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的对象。 非常感谢。

    1 回复  |  直到 1 年前
        1
  •  1
  •   artiomi    1 年前

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

    public interface CustomerRepository extends MongoRepository<Customer, String> {
       List<Customer> findAllByCustomerIdIn(List<String> customerIds);
    }