mongodb 操作工具类

news/2025/2/15 16:09:17/

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

}


http://www.ppmy.cn/news/526453.html

相关文章

java开发任务必备的工具_Web常用开发工具有哪些?常用工具推荐

Web常用开发工具有哪些?常用工具推荐&#xff0c;IT程序员为了快速、高效地完成任务&#xff0c;会使用一些Web开发具来辅助完成工作&#xff0c;这些工具有代码高亮显示、语法提示等便捷功能的前端开发工具&#xff0c;对于开发者非常友好。 小编整理了一些Web常用开发工具推…

php+mysql工具_PHP+MYSQL开发工具及资源收藏

PHP编辑工具 DzSoftPHPEditor 专为 PHP 所设计的网页程序编辑软件&#xff0c;具有 PHP 编辑、侦错、浏览、原始码检视、档案浏览、可自订的原始码样本等功能&#xff0c;无须架设网站主机就可以测试 PHP 下载地址&#xff1a; http://down.chinaz.com/s/8174.asp EngInSitePHP…

嵌入式开发<网络调试工具>

嵌入式开发<网络调试工具> 前言1&#xff0c;设备参数分类2&#xff0c;设备参数修改3&#xff0c;调试工具软件 一、软件界面二、功能说明1.网络设置1&#xff09;TCP Client设置A&#xff0c; 协议类型&#xff1a;B&#xff0c; 远程主机地址&#xff1a;C&#xff0c…

CM4_HDMI视频采集卡操作演示

关键词&#xff1a;树莓派 Compute Module 4 CM4 视频采集 推流 HDMI 输入 编码 实时预览 概述&#xff1a;这几年直播市场和网课市场都非常红火&#xff0c;对这两类应用都有一个需求&#xff0c;就是高清推流和录制&#xff0c;传统的PC方案或者采集卡方案需要虽然性能…

摄像头视频采集卡输入数据格式参数查询

可以使用potplay->打开->设备设置界面。当多个摄像头的时候&#xff0c;设备里面下拉菜单可以选择多项。在格式中查询参数。使用打开设备&#xff0c;可以看到设备视频图片。&#xff08;mark&#xff0c;防止自己以后忘记&#xff09;

【视频采集方案】

Android视频采集,传输,编码解码的方案总结 ipcamera-for-android 服务器 : Android手机充当服务器&#xff0c;使用NanoHTTPD充当服务器 客户端 &#xff1a; 手机或者pc输入http://server ip:8080观看。 这种方案可以参考 ipcamera-for-android开源项目&#xff0c;网址 …

Matorx视频采集卡开发及使用(一)

一、前言 本文初始目的为购买Matrox视频采集卡&#xff0c;用于采集Basker相机视频图像&#xff0c;接口为cameraLink。采集卡版本为ev-cl。二、环境搭载 本文搭载系统环境为Win10系统&#xff0c;软件为MIL&#xff08;Matrox Imaging Library&#xff09;。 安装包为厂家提…

图像采集卡的概念及作用原理

图像采集卡&#xff08;Image Grabber&#xff09;又称为图像卡&#xff0c;它将摄像机的图像视频信号&#xff0c;以帧为单位&#xff0c;送到计算机的内存和VGA帧存&#xff0c;供计算机处理、存储、显示和传输等使用&#xff1b;在机器视觉系统中&#xff0c;图像卡采集到的…