JAVA操作Mongo 数组模糊查询
引入mongo-java-driver-3.0.4 jar
工具类
//mongodb 连接数据库工具类
public class MongoDBUtil {
//不通过认证获取连接数据库对象
public static MongoDatabase getConnect(String database){
//连接到 mongodb 服务
MongoClient mongoClient = new MongoClient("localhost", 27017);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase(database);
//返回连接数据库对象
return mongoDatabase;
}
//需要密码认证方式连接
public static MongoDatabase getConnect2(String database){
List<ServerAddress> adds = new ArrayList<ServerAddress>();
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost", 27017);
adds.add(serverAddress);
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
credentials.add(mongoCredential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(adds, credentials);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase(database);
//返回连接数据库对象
return mongoDatabase;
}
}
条件查询
HashMap<String, Object> resultMap = new HashMap<String, Object>();
List<Document> result = new ArrayList<Document>();
MongoCollection<Document> collection = MongoDBUtil.getConnect("ccbfw").getCollection("fwpolicy");
Bson findQuery = null;
if (StringUtils.isNotBlank(page.getDevice_name() == null ? "" : page.getDevice_name())) {
Bson deviceName = Filters.and(deviceNameCondition(page.getDevice_name().trim()));
if (findQuery != null) {
findQuery = Filters.and(findQuery, Filters.and(deviceName));
} else {
findQuery = Filters.and(deviceName);
}
}
if (StringUtils.isNotBlank(page.getPolicy_id() == null ? "" : page.getPolicy_id())) {
Bson policyId = Filters.and(policyIdCondition(page.getPolicy_id().trim()));
if (findQuery != null) {
findQuery = Filters.and(findQuery, Filters.and(policyId));
} else {
findQuery = Filters.and(policyId);
}
}
if (StringUtils.isNotBlank(page.getService() == null ? "" : page.getService())) {
Set set = new HashSet();
//模糊匹配
Pattern pattern = Pattern.compile("^.*" + page.getService().trim() + ".*$", Pattern.CASE_INSENSITIVE);
set.add(pattern);
Bson service = Filters.in("service",set);
if (findQuery != null) {
findQuery = Filters.and(findQuery, Filters.and(service));
} else {
findQuery = Filters.and(service);
}
}
if (StringUtils.isNotBlank(page.getSource_address() == null ? "" : page.getSource_address())) {
Set set = new HashSet();
//模糊匹配
Pattern pattern = Pattern.compile("^.*" + page.getSource_address().trim() + ".*$", Pattern.CASE_INSENSITIVE);
set.add(pattern);
Bson source_address = Filters.in("source_address",set);
if (findQuery != null) {
findQuery = Filters.and(findQuery, Filters.and(source_address));
} else {
findQuery = Filters.and(source_address);
}
}
if (StringUtils.isNotBlank(page.getDest_address() == null ? "" : page.getDest_address().trim())) {
Set set = new HashSet();
//模糊匹配
Pattern pattern = Pattern.compile("^.*" + page.getDest_address().trim() + ".*$", Pattern.CASE_INSENSITIVE);
set.add(pattern);
Bson dest_address = Filters.in("dest_address", set);
if (findQuery != null) {
findQuery = Filters.and(findQuery, Filters.and(dest_address));
} else {
findQuery = Filters.and(dest_address);
}
}
MongoCursor<Document> cursor = null;
if (findQuery != null) {
cursor = collection.find(findQuery).sort(new Document("_id", -1)).skip(page.getFirstPage())
.limit(page.getRows()).iterator();
resultMap.put("total", collection.count(findQuery));
} else {
cursor = collection.find().sort(new Document("_id", -1)).skip(page.getFirstPage()).limit(page.getRows())
.iterator();
resultMap.put("total", collection.count());
}
try {
while (cursor.hasNext()) {
Document item = cursor.next();
result.add(item);
// System.out.println(item.toJson());
}
} finally {
cursor.close();// must be
}
resultMap.put("rows", result);
String json = Json.toJson(resultMap, JsonFormat.compact());
文档元素为数组的条件 匹配
int ipInt=IpTest.ipToInt(page.getSource_address().trim());
Bson condition_start=Filters.and(Filters.lte("startIp", ipInt));
Bson condition_end=Filters.and(Filters.gte("endIp", ipInt));
Bson condition=Filters.elemMatch("source_address", Filters.and(condition_start,condition_end));
if (findQuery != null) {
findQuery = Filters.and(findQuery,condition);
} else {
findQuery = Filters.and(condition);
}