Repository query with a List parameter in Spring Data MongoDB

前端 未结 2 1191
野趣味
野趣味 2021-02-01 15:59

I have the following POJO.

@Document(collection = \"questions\")
public class Question {

    @Id
    private String id;

    public List getTags()         


        
相关标签:
2条回答
  • 2021-02-01 16:40

    I will answer my own question as I have just found the answer by myself. The following section in the Spring Data MongoDB documentation lists all supported keywords that are used by Spring for its query derivation:

    http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repository-query-keywords

    The following implementation works for the use case described above:

    @Repository
    public interface QuestionRepository extends MongoRepository<Question, String> {
         List<Question> findByTagsIn(List<String> tags);
    }
    
    0 讨论(0)
  • 2021-02-01 16:51

    The CONTAINING keyword may also be used:

    @Repository
    public interface QuestionRepository extends MongoRepository<Question, String> {
         List<Question> findByTagsContaining(List<String> tags);
    }
    

    example and how it's mongo query looks like:

    findByAddressesContaining(Address address)
    
    {"addresses" : { "$in" : address}}
    

    This can also accept list of address in params.

    See documentation: https://github.com/spring-projects/spring-data-mongodb/blob/e28bede416e4ddac19a35dc239388afc90b9cac4/src/main/asciidoc/reference/mongo-repositories.adoc

    0 讨论(0)
提交回复
热议问题