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);
}
JAVA操作Mongo 数组模糊查询的更多相关文章
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)
1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...
- Java数据库学习之模糊查询(like )
Java数据库学习之模糊查询(like ): 第一种方式:直接在SQL语句中进行拼接,此时需要注意的是parm在SQL语句中需要用单引号拼接起来,注意前后单引号之间不能空格 String sql = ...
- java使用elasticsearch进行模糊查询-已在项目中实际应用
java使用elasticsearch进行模糊查询 使用环境上篇文章本人已书写过,需要maven坐标,ES连接工具类的请看上一篇文章,以下是内容是笔者在真实项目中运用总结而产生,并写的是主要方法和思路 ...
- php提供一维数组模糊查询
2019年9月30日14:36:15 提供一维数组模糊查询,只支持utf-8 内部处理是Unicode 编码特殊编码格式的可能会出错 if (!function_exists('arrayFuzzyQ ...
- java操作elasticsearch实现聚合查询
1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...
- java使用elasticsearch进行模糊查询之must使用-项目中实际使用
java使用elasticsearch进行多个条件模糊查询 文章说明: 1.本篇文章,本人会从java连接elasticsearch到查询结果生成并映射到具体实体类(涵盖分页功能) 2.代码背景:el ...
- python 操作mongodb数据库模糊查询
# -*- coding: utf-8 -*-import pymongoimport refrom pymongo import MongoClient #创建连接#10.20.66.106clie ...
- java~springboot~ibatis数组in查询的实现
在ibatis的xml文件里,我们去写sql语句,对应mapper类的方法,这些sql语句与控制台上没什么两样,但在有些功能上需要注意,如where in这种从数组里查询符合条件的集合里,需要在xml ...
- 如何在java List中进行模糊查询
比如我有下面这样一个List,里面存放的是多个Employee对象.然后我想对这个List进行按照Employee对象的名字进行模糊查询.有什么好的解决方案么? 比如我输入的查询条件为“wang”,那 ...
随机推荐
- [OpenCV实战]45 基于OpenCV实现图像哈希算法
目前有许多算法来衡量两幅图像的相似性,本文主要介绍在工程领域最常用的图像相似性算法评价算法:图像哈希算法(img hash).图像哈希算法通过获取图像的哈希值并比较两幅图像的哈希值的汉明距离来衡量两幅 ...
- [深度学习] tf.keras入门5-模型保存和载入
目录 设置 基于checkpoints的模型保存 通过ModelCheckpoint模块来自动保存数据 手动保存权重 整个模型保存 总体代码 模型可以在训练中或者训练完成后保存.具体文档参考:http ...
- 大数据实时多维OLAP分析数据库Apache Druid入门分享-上
@ 目录 概述 定义 MPP和Lambda补充说明 概述 特征 适用场景 不适用场景 横向对比 部署 单机部署 入门示例 概述 定义 Apache Druid 官网地址 https://druid.a ...
- java 入门与进阶P-6.5+P-6.6
字符串操作 字符串是对象,对它的所有操作都是通过" . " 这个运算符来进行的 字符串.操作 他表示对.左边的这个字符串做右边的那个操作 这里的字符串可以是变量也可以是常量 Str ...
- Linux密钥认证
网站集群批量管理-秘钥认证 一.概述 管理更加轻松:两个节点,通过秘钥形式进行访问,不需要输入密码,单向 应用场景: 一些服务在使用前要求做秘钥认证 手动写批量管理脚本 名字:秘钥认证,免密码登录,双 ...
- 深入解读.NET MAUI音乐播放器项目(一):概述与架构
系列文章将分步解读音乐播放器核心业务及代码: 深入解读.NET MAUI音乐播放器项目(一):概述与架构 深入解读.NET MAUI音乐播放器项目(二):播放内核 深入解读.NET MAUI音乐播放器 ...
- DataGrid 设置某列可见或只读
在ASP.NET中使用 DataGrid数据展示控件时,可以对数据进行展示,编辑,删除,在有些时候不希望某列被修改,进行如下设置 点击编辑后 想要如下效果 其中权限编码和权限分类不希望修改 设置方法 ...
- Javaweb-Tomcat(安装+配置环境)
Tomcat跟着教程,但是在bin目录下点击startup只能闪退的总结 1.先下载 直接搜tomcat下载就可以了(free) 2.解压到你想要的文件中 3.直接进入bin目录,找startup.b ...
- Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation
1.出现问题 查询数据时报如题错误 2.解决方案 建表时需指定字符集,例如: -- ---------------------------- -- Table structure for testTa ...
- PHY状态机分析
PHY的12种状态 enum phy_state { PHY_DOWN = 0, //关闭网卡 PHY_STARTING, //PHY设备准备好了,PHY driver尚为准备好 PHY_READY, ...