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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM