我有一个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);
}
这里是在projectTag字段中搜索searchCriteria值。我想在多个字段中搜索该值。例如另一个字段projectName
感谢您的协助。
答案 0 :(得分:1)
您可以在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);
答案 1 :(得分:0)
您可以使用以下代码
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);