精确查询忽略大小写
{display_name:/^test/i}
{ display_name": { "$regex" : "^test$", "$options" : "i" } }
Criteria.where("protocols").regex(Pattern.compile("^"+searchDTO.getProtocol()+"$", Pattern.CASE_INSENSITIVE))
模糊查询忽略大小写
db.SdnVmwareRule.find({display_name:{ "$regex" : "\\Qtest\\E", "$options" : "iu" }})
criteria.and("display_name").regex(Pattern.compile(Pattern.quote("test"), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE));
查询列表对象属性:
数据结构:{id:"",ruleObjectList:[{"srcIps":"1.1.1.1","dstIps":"2.2.2.2"}]}
db.SdnVmwareRule.find({ruleObjectList:{ $elemMatch: { srcIps:"20.35.30.16-20.35.30.23"} }})
Criteria.where("ruleObjectList").elemMatch(Criteria.where("srcPorts").is(searchDTO.getSrcPort()))
同一种属性查询两次要声明and包裹起来:ruleObjectList属性查询两次
db.SdnVmwareRule.find({ "_id" : "VM_SDN-2-2-8-rule", "display_name" : { "$regex" : "\\Qtt-subTask-8\\E", "$options" : "iu" }, "$and" :
[{ "ruleObjectList" : { "$elemMatch" : { "srcPorts" : "0-65535" } } }, { "ruleObjectList" : { "$elemMatch" : { "protocols" : { "$regex" : "\\Qudp\\E", "$options" : "i" } } } }] })
List<Criteria> list2 = new ArrayList<>();
list2.add(Criteria.where("ruleObjectList").elemMatch(Criteria.where("srcPorts").is(searchDTO.getSrcPort())));
list2.add(Criteria.where("ruleObjectList").elemMatch(Criteria.where("destPorts").is(searchDTO.getDstPort())));
list2.add(Criteria.where("ruleObjectList").elemMatch(Criteria.where("protocols").regex(Pattern.compile("^"+searchDTO.getProtocol()+"$", Pattern.CASE_INSENSITIVE))));
Criteria criteria = new Criteria();
criteria.and("_id").is(searchDTO.getObjectId());
criteria.and("display_name").regex(Pattern.compile(Pattern.quote(searchDTO.getRuleName()), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE));
criteria.andOperator(list2.toArray(new Criteria[0]));
Query query = new Query();
query.addCriteria(criteria);
出现多个or也需要用and包裹一层 同上
分页排序
Query query = new Query(); Criteria criteria = Criteria.where("id").is(id); query.addCriteria(criteria); query.skip((pageNum - 1) * pageSize); query.limit(pageSize); query.with(Sort.by( Sort.Order.asc("readOrNot"), Sort.Order.desc("sendTime") ));