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

用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”));


用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”));

你可能感兴趣的:(用Java实现MongoDB正则查询小技巧)