前言
有个需求是从MongoDB中查询差异不为0的数据,即根据正则匹配mongo中字段不为0。
提示:以下是本篇文章正文内容,下面案例可供参考
一、正则表达式匹配为0
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Test
public void pattern() {
String str = "0.000";
String pattern = "^[0.]*$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
System.out.println(m.matches()); // true
}
二、mongo中查询差异不为0
// 正则匹配为0的数据
Pattern compile = Pattern.compile("^[0.]*$");
// mongo的查询:差异不为 0 需加上 not 取反
Criteria criteria = new Criteria();
criteria.and("qtyStuDiff").not().regex(compile);
总结
我恋的不是雪,而是有你的冬天。