在Spring Data MongoDB中使用List参数的存储库查询
我有以下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;
}
}
我正在尝试实现一个查询,该查询查找包含标签列表的所有s。我尝试了以下方法:MongoRepository
Question
@Repository
public interface QuestionRepository extends MongoRepository<Question, String> {
List<Question> findByTags(List<String> tags);
}
但这只有在我传递给方法的标记完全匹配Mongo中分配给问题的标记列表时才有效。例如,如果我在Mongo中有一个问题,其中包含标签列表,当我传递给该方法时,它不会返回。List
[ "t1", "t2", "t3" ]
findByTags(List)
[ "t1", "t2" ]
我也尝试了以下方法:
@Repository
public interface QuestionRepository extends MongoRepository<Question, String> {
@Query("{ tags: { $all: ?0 } }")
List<Question> findByTags(List<String> tags);
}
但后来我的根本无法部署到我的 servlet 容器中。(在这种情况下,我收到以下错误:war
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.
请您就如何实现该自定义查询提供建议吗?