java 查询mongo模糊匹配

时间:2021-10-24 06:09:05
1完全匹配
Pattern pattern = Pattern.compile("^王$", Pattern.CASE_INSENSITIVE);
2右匹配
Pattern pattern = Pattern.compile("^.*王$", Pattern.CASE_INSENSITIVE);
3左匹配
Pattern pattern = Pattern.compile("^王.*$", Pattern.CASE_INSENSITIVE);
4模糊匹配

Pattern pattern = Pattern.compile("^.*王.*$", Pattern.CASE_INSENSITIVE);

BasicDBObject query = new BasicDBObject();
query.put("account_name", new BasicDBObject("$regex", pattern));