Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MongoDB在多个字段中搜索值_Java_Mongodb_Spring Mvc_Mongotemplate - Fatal编程技术网

Java MongoDB在多个字段中搜索值

Java MongoDB在多个字段中搜索值,java,mongodb,spring-mvc,mongotemplate,Java,Mongodb,Spring Mvc,Mongotemplate,我有一个java方法 public List<Project> tagSearch(String searchCriteria){ Query query = new Query(); return mongoTemplate.find(query.addCriteria(Criteria.where("projectTag") .regex(searchCriteria,"i")), Project.class, COLLECTION_NA

我有一个java方法

 public List<Project> tagSearch(String searchCriteria){
    Query query = new Query();
    return mongoTemplate.find(query.addCriteria(Criteria.where("projectTag")
            .regex(searchCriteria,"i")), Project.class, COLLECTION_NAME);
}
公共列表标记搜索(字符串搜索条件){
查询=新查询();
返回mongoTemplate.find(query.addCriteria(Criteria.where)(“projectTag”)
.regex(searchCriteria,“i”)、Project.class、集合名称);
}
在这里,它正在projectTag字段中搜索searchCriteria值。我想在多个字段中搜索该值。例如,另一个字段projectName

感谢您的帮助。

使用

您可以在orOperator中使用任意多个条件。然后它会像这样:

Query query = new Query();
Criteria criteria1 = new Criteria().where("projectTag").regex(searchCriteria, "i");
Criteria criteria2 = new Criteria().where("projectName").regex(searchCriteria, "i");
query.addCriteria(new Criteria().orOperator(c1,c2));
return mongoTemplate.find(query, Project.class, COLLECTION_NAME);
您可以使用下面的代码

final Criteria criteria = new Criteria();
criteria.orOperator(Criteria.where("projectTag").regex(searchCriteria, "i"),
                    Criteria.where("projectName").regex(searchCriteria, "i"));
final Query query = new Query(criteria);
final List<Project> projectEntities = mongoTemplate.find(query, Project.class);
最终标准=新标准();
标准.orOperator(标准.where(“projectTag”).regex(searchCriteria,“i”),
标准。其中(“项目名称”).regex(搜索标准,“i”);
最终查询=新查询(条件);
最终列表projectEntities=mongoTemplate.find(查询,Project.class);