Java Spring Data MongoDB 中带有 List 参数的存储库查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30123810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Repository query with a List parameter in Spring Data MongoDB
提问by Marchev
I have the following POJO.
我有以下 POJO。
@Document(collection = "questions")
public class Question {
@Id
private String id;
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
}
I am trying to implement a MongoRepository
query which finds all Question
s that contain a list of tags. I have tried the following:
我正在尝试实现一个MongoRepository
查询,该查询查找Question
包含标签列表的所有s。我尝试了以下方法:
@Repository
public interface QuestionRepository extends MongoRepository<Question, String> {
List<Question> findByTags(List<String> tags);
}
but this is only working when the List
of tags that I'm passing to the method fully matches the list of tags assigned to the question in Mongo. E.g. if I have a question in Mongo with a list of tags [ "t1", "t2", "t3" ]
it is not returned by findByTags(List)
when I pass [ "t1", "t2" ]
to the method.
但这仅在List
我传递给该方法的标签与 Mongo 中分配给问题的标签列表完全匹配时才有效。例如,如果我在 Mongo 中有一个带有标签列表的问题,当我传递给方法时[ "t1", "t2", "t3" ]
它不会返回。findByTags(List)
[ "t1", "t2" ]
I have tried the following as well:
我也尝试了以下方法:
@Repository
public interface QuestionRepository extends MongoRepository<Question, String> {
@Query("{ tags: { $all: ?0 } }")
List<Question> findByTags(List<String> tags);
}
but then my war
could not be deployed to my servlet container at all. (I get the following error in that case:
但是我war
根本无法部署到我的 servlet 容器。(在这种情况下,我收到以下错误:
The web application [backend] appears to have started a thread named [cluster-1-db:27017] but has failed to stop it. This is very likely to create a memory leak.
Would you please advise on how to implement that custom query?
您能否就如何实施该自定义查询提出建议?
采纳答案by Marchev
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:
我将回答我自己的问题,因为我刚刚自己找到了答案。Spring Data MongoDB 文档中的以下部分列出了 Spring 用于其查询派生的所有受支持的关键字:
http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repository-query-keywords
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);
}
回答by alegria
The CONTAINING keyword may also be used:
也可以使用 CONTAINING 关键字:
@Repository
public interface QuestionRepository extends MongoRepository<Question, String> {
List<Question> findByTagsContaining(List<String> tags);
}
example and how it's mongo query looks like:
示例以及它的 mongo 查询如下所示:
findByAddressesContaining(Address address)
{"addresses" : { "$in" : address}}
This can also accept list of address in params.
这也可以接受 params 中的地址列表。
See documentation: https://github.com/spring-projects/spring-data-mongodb/blob/e28bede416e4ddac19a35dc239388afc90b9cac4/src/main/asciidoc/reference/mongo-repositories.adoc
请参阅文档:https: //github.com/spring-projects/spring-data-mongodb/blob/e28bede416e4ddac19a35dc239388afc90b9cac4/src/main/asciidoc/reference/mongo-repositories.adoc