我有以下几点建议.

@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;
    }
}

我正在try 实现一个MongoRepository查询,它将查找所有包含标签列表的Question个.我try 了以下方法:

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

但只有当我传递给该方法的List个标记与Mongo中分配给问题的标记列表完全匹配时,这才有效.例如,如果我在Mongo中有一个带有标签[ "t1", "t2", "t3" ]列表的问题,当我将[ "t1", "t2" ]传递给该方法时,findByTags(List)不会返回该问题.

我也try 过以下方法:

@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.

请您就如何实现该定制查询提供建议?

推荐答案

我会回答我自己的问题,因为我刚刚找到了答案.Spring Data MongoDB文档中的以下部分列出了Spring用于查询派生的所有受支持的关键字:

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

以下实现适用于上述用例:

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

Mongodb相关问答推荐

MongoDB聚合匹配字符串字符

如何将数组$拉到对象数组下

用Spring Boot查询MongoDB中的对象数组

如何在 MongoDB 中的集合下查找同一文档

将子元素的数组值提取到 mongodb 中的单个数组中?

Mongoose 聚合和多级组

如何修改 mongodb 中嵌套对象数组中的字段名称/键?

在数据中只有月份和年份的 mongodb 中字符串的日期时间

Mongoose 和 MongoDB,如何在具有多个引用的两个模型之间创建关系?

MongoDB:插入重复键更新

Node.js 和 Passport 对象没有方法 validPassword

MongoDB:单个数据库处理程序的 >5 个打开连接

无法连接到远程 mongodb 服务器

如何使用 angular.js 推送通知?

如何在 MongoDB 的 $match 中使用聚合运算符(例如 $year 或 $dayOfMonth)?

Flask:设置应用程序和请求特定的属性?

如何在 MongoDB 中删除此弃用警告,为什么会这样?

PyMongo 中的警告消息:count is deprecated

停止副本集 MongoDB

全局初始化失败:BadValue Invalid or no user locale set.请确保正确设置 LANG 和/或 LC_* 环境变量