MongoDB初探系列之四:MongoDB与Java共舞
因为版本号不同,可能API也有所不同。本次学习用的是3.0版本号。
1、使用的mongodb的jdbc驱动版本号为:mongo-java-driver-3.0.0.jar
2、本节仅仅是简介JDBC操作,临时不考虑效率问题。
3、封装的工具类代码例如以下:
public class MongoDBProxy { private static MongoDBProxy proxy=null;//单实例
private static MongoDatabase db=null;//数据库连接对象
private static String [] paramArray=new String[5];//数据库连接參数
private MongoDBProxy(){ }
static{
paramArray[0]="username";
paramArray[1]="password";
paramArray[2]="host";
paramArray[3]="port";
paramArray[4]="databaseName";
}
/**
* 得到MongoDBProxy
* 採用系统默认配置
*/
public static MongoDBProxy getMongoDBProxy(){
if(proxy==null){
proxy=new MongoDBProxy();
String sURI = String.format("mongodb://%s:%s@%s:%d/%s",paramArray[0],paramArray[1],paramArray[2],Integer.parseInt(paramArray[3]),paramArray[4]);
MongoClientURI uri = new MongoClientURI(sURI);
MongoClient mongoClient = new MongoClient(uri);
db= mongoClient.getDatabase(paramArray[4]);
}
return proxy;
}
/**
* 批量查询数据
* @param table 集合名称
* @param page 分页參数
* @param filter 过滤条件
* @param sort 排序条件
*/
public Page findDocList(String table,Page page,Bson filter,Bson sort){
MongoCollection<Document> coll=db.getCollection(table);
long count=coll.count(filter);//依据过滤条件获取数据总量
Page p=PageUtil.createPage(page,Integer.parseInt(String.valueOf(count)));
p.setFromUrl((page.getFromUrl()==null)?"":page.getFromUrl());
p.setParamString((page.getParamString()==null)?"":page.getParamString());
FindIterable<Document> resultIterable=coll.find();
//运行条件过滤
resultIterable=resultIterable.sort(sort).filter(filter).skip(p.getBeginIndex()).batchSize(p.getEveryPage());
MongoCursor<Document> cousor=resultIterable.iterator();
List<Document> dataList=new ArrayList<Document>();
while(cousor.hasNext()){
dataList.add(cousor.next());
}
p.setDataList(dataList);
return PageUtil.buildPageString(p);
}
/**
* 获取单个文档
* @param table 集合名称
* @param filter 过滤条件
* @param sort 排序条件
*/
public Document findOneDoc(String table,Bson filter,Bson sort){
MongoCollection<Document> coll=db.getCollection(table);
FindIterable<Document> resultIterable=coll.find();
if(sort!=null){
resultIterable.sort(sort);
}
if(filter!=null){
resultIterable.filter(filter);
}
return resultIterable.first();
}
/**
* 加入文档
* @param table 集合名称
* @prama doc 文档内容
*/
public void addDocument(String table,Document doc){
MongoCollection<Document> coll=getCollection(table);
coll.insertOne(doc);
}
/**
* 批量加入文档
* @param table 集合名称
* @prama docList 文档集合
*/
public void addDocumentList(String table,List<Document> docList){
MongoCollection<Document> coll=getCollection(table);
coll.insertMany(docList);
}
/**
* 更新文档
* @param table 集合名称
* @param query 查询条件
* @param up 更新数据
*/
public UpdateResult updateDocument(String table,Bson query,Bson up){
MongoCollection<Document> coll=getCollection(table);
return coll.updateOne(query,up);
}
/**
* 替换文档
* @param table 集合名称
* @param query 查询条件
* @param up 替换的文件对象
*/
public UpdateResult replaceDocument(String table,Bson query,Document up){
MongoCollection<Document> coll=getCollection(table);
return coll.replaceOne(query, up);
}
/**
* 删除文档
* @param table 集合名称
* @param delete 删除条件
*/
public DeleteResult deleteDocument(String table,Bson delete){
MongoCollection<Document> coll=getCollection(table);
return coll.deleteOne(delete);
}
/**
* 获取集合对象
* @param table 集合名称
*/
private MongoCollection<Document> getCollection(String table){
return db.getCollection(table);
}
}
4、调用demo
MongoDBProxy proxy=MongoDBProxy.getMongoDBProxy();
System.out.println(proxy.findOneDoc("users",null,null).get("_id"));
Document doc=new Document();
doc.put("user","李四");
proxy.addDocument("users", doc);
Bson bson=new BasicDBObject("user","张三");
proxy.deleteDocument("users", bson);
兴许再深入学习,先用demo上上手哇。哈哈。
MongoDB初探系列之四:MongoDB与Java共舞的更多相关文章
- MongoDB初探系列之二:认识MongoDB提供的一些经常使用工具
在初探一中,我们已经能够顺利的将MongoDB在我们自己的机器上跑起来了. 可是在其bin文件夹以下另一些我们不熟知的工具.接下来,将介绍一下各个小工具的用途以及初探一中MongoDB在data文件夹 ...
- MongoDb进阶实践之四 MongoDB查询命令详述
一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命名和删除.有了这些基 ...
- MongoDb进阶实践之六 MongoDB查询命令详述(补充)
一.引言 上一篇文章我们已经介绍了MongoDB数据库的查询操作,但是并没有介绍全,随着自己的学习的深入,对查询又有了新的东西,决定补充进来.如果大家想看上一篇有关MongoDB查询的 ...
- java mongodb 基础系列---查询,排序,limit,$in,$or,输出为list,创建索引,$ne 非操作
官方api教程:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started ...
- mongodb基础系列——数据库查询数据返回前台JSP(一)
经过一段时间停顿,终于提笔来重新整理mongodb基础系列博客了. 同时也很抱歉,由于各种原因,没有及时整理出,今天做了一个demo,来演示,mongodb数据库查询的数据在JSP显示问题. 做了一个 ...
- MongoDB学习笔记-2(使用java连接Mongo)
本人使用maven创建的java工程,mongo-java-driver使用的是3.2.2,这个驱动2.x和3.x是有差异的 pom.xml配置加入: <dependency> <g ...
- MongoDb进阶实践之八 MongoDB的聚合初探
一.引言 好久没有写东西了,MongoDB系列的文章也丢下好长时间了.今天终于有时间了,就写了一篇有关聚合的文章.一说到“聚合”,用过关系型数据库的人都应该知道它是一个什么东西.关系型数据库有“聚合” ...
- MongoDB学习之(二)java连接
上一章完了下mongodb的安装和IDE工具,现在开始使用java进行连接. 第一步:使用jar包, 这里需要三个包,具体为啥我也不清楚,反正因为报错,我就按照官方文档一个个的都下载了. 链接:htt ...
- MongoDB干货系列2-MongoDB执行计划分析详解(2)(转载)
写在之前的话 作为近年最为火热的文档型数据库,MongoDB受到了越来越多人的关注,但是由于国内的MongoDB相关技术分享屈指可数,不少朋友向我抱怨无从下手. <MongoDB干货系列> ...
随机推荐
- Hdu-6252 2017CCPC-Final J.Subway Chasing 差分约束
题面 题意:有2个人,都去坐地铁,但是他们相差了X分钟,但是他们也一直在通讯,于是你就知道,你在AB站点中间的时候,他在CD中间,(B一定等于A+1或者A,同理D也是),问你每2个站之间需要的时间的一 ...
- windows下flink示例程序的执行
1.什么是flink Apache Flink® - Stateful Computations over Data Streams 2.启动 下载地址 我下载了1.7.2 版本 解压到本地文件目 ...
- C - Insomnia cure
Problem description «One dragon. Two dragon. Three dragon», — the princess was counting. She had tro ...
- WordPress音乐主题Always1.8
WordPress响应式主题 音乐主题个人博客杂志主题Always主题V1.8 Always主题V1.8是以Ajax加以CSS动画的方式,很好的将优雅的设计感和极度精简的代码同时表现了出来,进而缔造出 ...
- CUDA 编程实例:计算点云法线
程序参考文章:http://blog.csdn.net/gamesdev/article/details/17535755 程序优化2 简介:CUDA ,MPI,Hadoop都是并行运算的工具.CU ...
- Java中数组的概念与特点
数组概念: 数组其实也是一个容器,可以用来存储固定个数相同类型的数据数组的定义 数组中存储的数据叫做元素 特点: 1.数组是引用数据类型 2.数组的长度是固定的,也就是说可以存储固定个数的数据 3.数 ...
- vc++如何创建程序-构造函数02
1.若忘记了赋值,出现运行结果是很大的负值(因为我们定义的x与y这两个成员变量存储在内存中是一个随机的值) 当我们调用时,随机输出. //包含输入输出的头文件#include<iostream. ...
- loadrunner中的常见问题
1.Loadrunner参数化默认只显示100条数据,我们如何改变呢 E:\Program Files (x86)\HP\LoadRunner\config 2.如何突破loadrunner的Cont ...
- ASCII 码对照表
ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ! 65 A 97 a 2 ST ...
- 【模板】 非旋转treap
模板:luogu P3369 [模板]普通平衡树 code: #include <cstdio> #include <cstdlib> const int MAX_N=1000 ...