String content = "192.168.1.2";//匹配条件
Pattern compile = Pattern.compile("^((?!" + content+ ").)*$", Pattern.CASE_INSENSITIVE);
Query query = new Query().skip(0).limit(100);
// 查询 imageUrl 不包含 content 的数据
query.addCriteria(Criteria.where("imageUrl").is(compile));
List persons = mongoTemplate.find(query, Person.class);
String content = "192.168.1.2";//匹配条件
Pattern pattern = Pattern.compile("^.*" + content + ".*$",Pattern.CASE_INSENSITIVE);
Query query = new Query().skip(0).limit(100);
// 查询 imageUrl 包含 content 的数据
query.addCriteria(Criteria.where("imageUrl").is(pattern));
List persons = mongoTemplate.find(query, Person.class);
String content = "192.168.1.2";//匹配条件
Pattern pattern = Pattern.compile("^.*" + content + ".*$",Pattern.CASE_INSENSITIVE);
Query query = new Query();
Criteria criteria = new Criteria();
// 查询 imageUrl 和 image 有至少有一个包含 content 的数据
criteria.orOperator(Criteria.where("imageUrl").is(pattern),Criteria.where("image").is(pattern));
query.addCriteria(criteria);
List persons = mongoTemplate.find(query, Person.class);
String content = "192.168.1.2";//匹配条件
Pattern pattern = Pattern.compile("^.*" + content + ".*$",Pattern.CASE_INSENSITIVE);
Query query = new Query();
Criteria criteria = new Criteria();
// 查询 imageUrl 和 image 都包含 content 的数据
criteria.andOperator(Criteria.where("imageUrl").is(pattern),Criteria.where("image").is(pattern));
query.addCriteria(criteria);
List persons = mongoTemplate.find(query, Person.class);
如果有用,求关注 感谢