mongodb 操作工具类

package com.jk.utils;

import java.util.ArrayList;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.sun.org.apache.bcel.internal.generic.DCONST;

public class MongoDBUtil {

/*** mongodb数据库地址    192.168.1.188*/
private static String host = "192.168.1.175";
/*** 端口号*/
private static Integer port = 27017;private static MongoClient mongoClient = null;
static{mongoClient = new MongoClient(host, port);MongoClientOptions.Builder builder = new MongoClientOptions.Builder();builder.connectionsPerHost(300); // 连接池设置为300个连接,默认为100builder.connectTimeout(15000);//连接超时,推荐>3000毫秒builder.maxWaitTime(5000);//最大等待时间builder.socketTimeout(0);//套接字超时时间,0无限制builder.threadsAllowedToBlockForConnectionMultiplier(5000);// 线程队列数,如果连接线程排满了队列就会抛出“Out of semaphores to get db”错误。builder.build();
}/*** * 方法: findOneById <br>* 描述: 通过id查询单条数据 <br>* 作者: Teacher song<br>* 时间: 2017-4-22 上午11:31:46* @param dbName 数据库名称* @param coll     集合名称* @param id        id* @return    Document*/
public static Document findOneById(String dbName,String coll,String id){MongoCollection<Document> collection = getColls(dbName, coll);Document document = new Document();document.put("_id", new ObjectId(id));FindIterable<Document> find = collection.find(document);MongoCursor<Document> iterator = find.iterator();if (iterator.hasNext()) {return iterator.next();}else{return null;}
}
/*** * 方法: add <br>* 描述: 保存单条数据 <br>* 作者: Teacher song<br>* 时间: 2017-4-21 下午3:06:43* @param dbName   数据库名字* @param coll     集合名字* @param document  保存的document*/
public static void add(String dbName,String coll,Document document){MongoCollection<Document> collection = getColls(dbName, coll);collection.insertOne(document);
}
/*** * 方法: getDB <br>* 描述: 获取collection <br>* 作者: Teacher song<br>* 时间: 2017-4-21 下午3:10:07* @param dbName   数据库名* @param coll  集合名* @return*  MongoCollection<Document>*/
private static MongoCollection<Document> getColls(String dbName, String coll) {MongoDatabase database = mongoClient.getDatabase(dbName);MongoCollection<Document> collection = database.getCollection(coll);return collection;
}
/*** * 方法: delByIds <br>* 描述: 根据id批量删除 <br>* 作者: Teacher song<br>* 时间: 2017-4-21 下午3:09:04* @param dbName* @param coll* @param ids*/
public static long delByIds(String dbName,String coll,String ... ids){MongoCollection<Document> colls = getColls(dbName, coll);Document document = new Document();Document document2 = new Document();ArrayList<ObjectId> arrayList = new ArrayList<ObjectId>();for (int i = 0; i < ids.length; i++) {arrayList.add(new ObjectId(ids[i]));}document2.put("$in", arrayList);document.put("_id", document2);DeleteResult deleteMany = colls.deleteMany(document);long deletedCount = deleteMany.getDeletedCount();return deletedCount;
}/*** * 方法: updateById <br>* 描述: 根据id修改 <br>* 作者: Teacher song<br>* 时间: 2017-4-21 下午3:21:26* @param dbName   数据库名称* @param coll     集合名称* @param id        需要修改的数据id* @param document 需要修改的内容* @return*/
public static UpdateResult updateById(String dbName,String coll,String id,Document document){MongoCollection<Document> colls = getColls(dbName, coll);Document where = new Document();where.put("_id", new ObjectId(id));UpdateResult updateMany = colls.updateMany(where, document);return updateMany;
}
/*** * 方法: find <br>* 描述: 查询带分页排序 <br>* 作者: Teacher song<br>* 时间: 2017-4-21 下午3:30:15* @param dbName 数据库名字* @param coll     集合名字* @param where     搜索条件* @param page      页数* @param rows        每页条数* @param isSort    是否排序* @param sortName  排序字段(如果isSort为false的时候 ,该字段不生效)* @param sort     排序方式(1:正序 -1:倒序【如果isSort为false的时候 ,该字段不生效】)* @return   MongoCursor<Document>*/
public static MongoCursor<Document> find(String dbName,String coll,Document where,Integer page,Integer rows,boolean isSort,String sortName,Integer sort){MongoCollection<Document> colls = getColls(dbName, coll);FindIterable<Document> find = null;//搜索条件if (where != null && !where.isEmpty()) {find = colls.find(where);}else{find = colls.find();}//排序if (isSort) {Document sortFlag = new Document();sortFlag.put(sortName, sort);find.sort(sortFlag);}FindIterable<Document> limit = find.skip((page-1) * rows).limit(rows);MongoCursor<Document> iterator = limit.iterator();return iterator;
}
/*** * 方法: getCount <br>* 描述: 查询总数 <br>* 作者: Teacher song<br>* 时间: 2017-4-21 下午3:35:49* @param dbName    数据库名称* @param coll 集合名称* @param where 筛选条件* @return  Integer*/
public static Integer getCount(String dbName,String coll,Document where){MongoCollection<Document> colls = getColls(dbName, coll);if (where != null && !where.isEmpty()) {return (int) colls.count(where);}else{return (int) colls.count();}
}

}

mongodb 操作工具类相关推荐

  1. 字符串工具类、数组工具类、集合工具类、转型操作工具类、编码与解码操作工具类...

    package hjp.smart4j.framework.util;import org.apache.commons.lang3.StringUtils;/*** 字符串工具类*/ public ...

  2. XML文档操作工具类

    1 /// <summary> 2 /// XML文档操作工具类 3 /// </summary> 4 public class XmlUtil 5 { 6 #region X ...

  3. Code片段 : .properties属性文件操作工具类 JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! "贵专" - 泥瓦匠 一.java.util.Properties API ...

  4. 文件操作工具类FileUtil

    分享一个文件处理的工具类,依赖如下: <dependency><groupId>org.apache.ant</groupId><artifactId> ...

  5. geotools读取shp文件及shp文件操作工具类代码

    geotools读取shp文件及shp文件操作工具类代码.pdf 完整文档下载地址 https://download.csdn.net/download/a772304419/17468931 imp ...

  6. JAVA——文件操作工具类封装的简单实现

    问题描述 关于文件操作的简单工具类,包括文件夹创建,文件夹删除,文件创建,文件重命名,文件复制,文件删除.如果需要文件夹复制,其实就是创建文件夹和复制文件的操作. 解决方案 上下文 package c ...

  7. mysql util_关于mysql数据库操作工具类MySQLUtils用于连接数据提交sql脚本及结果转为JSONArray等操作...

    一.前言 关于实现mysql数据简单操作工具类MySQLUtils,用户密码连接数据库执行sql脚本ExecSQL.将查询结果转为com.alibaba.fastjson.JSONArray(fast ...

  8. Mysql和Oracle 数据库操作工具类

    适配Mysql和Oracle数据源 文章目录 1. 适配Mysql和Oracle数据源 2. 适配于Mysql数据源 3. 适配Oeacle数据源 1. 适配Mysql和Oracle数据源 packa ...

  9. java/javascript 时间操作工具类

    一.java 时间操作工具类 import org.springframework.util.StringUtils;import java.text.ParseException; import j ...

最新文章

  1. python * 与 ** 分别代表元组和字典
  2. Bimsight视图及导航控制
  3. Halcon初学者知识【15】图像的定义域Domain
  4. matlab RBF 神经网络拟合
  5. 计算机应用基础实训任务书,《计算机应用基础》任务书
  6. linux网络编程之一般应用采用的协议和不同套接字的地址结构以及用户进程和内核通过哪些函数传递套接字的地址结构
  7. Hibernate READ_ONLY CacheConcurrencyStrategy如何工作
  8. access驱动程序_Linux驱动程序学习二 (续) scull 源码在内核5.4.0上的编译调试
  9. Dlib 19.14发布——增加了一个训练RBF-SVM的auto-ML工具
  10. java远程执行jmi,java调用matlab 时出现java.lang.NullPointerException错误
  11. 固定大小采样池中的随机采样证明
  12. python 迭代器 生成器 区别_Python的生成器和迭代器之间的区别
  13. Elastic ik插件配置热更新功能
  14. ADS2020 Crack使用教程
  15. RC电路延时公式推导
  16. App中WebView网页加载优化实战干货
  17. CVPR 2020——OccuSeg: Occupancy-aware 3D Instance Segmentation
  18. 不要与最好的朋友合伙开公司?对吗
  19. 随机位置生成小方块案例
  20. 超链接之锚点的使用(页面内段落之间的跳转和不同页面之间的跳转)

热门文章

  1. python闰年表达式_Python中的闰年程序
  2. 职业经理人的五大工作
  3. 【云驻共创】云原生应用架构之企业核心业务未来架构演进路线及华为云方案
  4. 安卓高德地图聚合点击事件_滴滴进攻,华为入场,互联网地图迎来大变局|深响独家...
  5. flutter 生成二维码,中心可加图片
  6. 【愚公系列】2023年06月 网络安全(交通银行杯)-数据包里有甜甜圈哦~
  7. 好惨一恐龙:在最后时刻,它们将所有灾难都经历个遍
  8. 《内网安全攻防:渗透测试实战指南》读书笔记(七):跨域攻击分析及防御
  9. 服装收银软件排行榜新鲜出炉,码住这一篇就够了!
  10. 团队敏捷实践:迭代演示会议规则--用户故事