【发布时间】:2015-07-19 08:50:39
【问题描述】:
我有以下 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;
}
}
我正在尝试实现一个MongoRepository 查询,它可以找到所有包含标签列表的Questions。我尝试了以下方法:
@Repository
public interface QuestionRepository extends MongoRepository<Question, String> {
List<Question> findByTags(List<String> tags);
}
但这仅在我传递给该方法的标签的List 与分配给Mongo 中的问题的标签列表完全匹配时才有效。例如。如果我在 Mongo 中有一个带有标签列表 [ "t1", "t2", "t3" ] 的问题,当我将 [ "t1", "t2" ] 传递给该方法时,findByTags(List) 不会返回它。
我也尝试了以下方法:
@Repository
public interface QuestionRepository extends MongoRepository<Question, String> {
@Query("{ tags: { $all: ?0 } }")
List<Question> findByTags(List<String> tags);
}
但是我的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.
您能否就如何实现该自定义查询提出建议?
【问题讨论】: