java mongo 数组_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 adds = new ArrayList();

//ServerAddress()两个参数分别为 服务器地址 和 端口

ServerAddress serverAddress = new ServerAddress("localhost", 27017);

adds.add(serverAddress);

List credentials = new ArrayList();

//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 resultMap = new HashMap();

List result = new ArrayList();

MongoCollection 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 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());

文档元素为数组的条件 匹配

8f42941f189fff7a90afad600adab54f.png

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值