Mongotemplate多种情景模糊查询

按照姓名精确查询

Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
按照姓名模糊查询
Pattern pattern=Pattern.compile("^.*"+pattern_name+".*$", Pattern.CASE_INSENSITIVE);
query.addCriteria(Criteria.where("name").regex(pattern));
多条件查询
Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
query.addCriteria(Criteria.where("age").is(age));

或者另外一种写法

Criteria criteria = new Criteria();
criteria.and("name").is(name);
criteria.and("_id").is(new ObjectId(id));
Query query = new Query(criteria);
一个模糊关键字匹配多字段
 Pattern pattern=Pattern.compile("^.*"+pattern_name+".*$", Pattern.CASE_INSENSITIVE);
            criatira.orOperator(Criteria.where("name").regex(pattern),
                    Criteria.where("sex").regex(pattern),
                    Criteria.where("age").regex(pattern),
                    Criteria.where("class").regex(pattern));
精确匹配同上

 
  

你可能感兴趣的:(Mongotemplate多种情景模糊查询)