当前位置:Gxlcms > mysql > 用Java实现MongoDB正则查询小技巧

用Java实现MongoDB正则查询小技巧

时间:2021-07-01 10:21:17 帮助过:39人阅读

用Java实现MongoDB正则查询小技巧: //模糊查询 private BasicDBObject getLikeStr(String findStr) { Pattern pattern = Pa

用Java实现MongoDB正则查询小技巧:

//模糊查询
private BasicDBObject getLikeStr(String findStr) {
Pattern pattern = Pattern.compile("^.*" + findStr + ".*$", Pattern.CASE_INSENSITIVE);
return new BasicDBObject("$regex", pattern);
}

DBObject query = new BasicDBObject();
query.put("real_name", getLikeStr(userName));


//endWith文件扩展名
private BasicDBObject endWithStr(String findStr) {
Pattern pattern = Pattern.compile(findStr + "$", Pattern.MULTILINE);
return new BasicDBObject("$regex", pattern);
}

DBObject query = new BasicDBObject();
query.put("filename", endWithStr(“.pdf”));

linux

人气教程排行