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干货系列> ...
随机推荐
- js设计模式-享元模式
享元模式实际上是一种优化模式,目的在于提高系统的性能和代码的效率. 使用享元模式的条件:最重要的条件是网页中必须使用了大量资源密集型对象,如果只会用到了少许这类对象,那么这种优化并不划算.第二个条件是 ...
- 鸟哥的Linux私房菜笔记第六章(一)
目录与路径 相对路径与绝对路径 上一章简单的提到绝对路径和相对路径 绝对路径:路径的写法一定是由根目录(/)写起的,例如:/home/user 这个目录 相对路径:路径的写法不是由根目录(/)写起,例 ...
- B - Letter(最小覆盖矩形)
Problem description A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sh ...
- Nginx介绍及知识点(摘抄)
正向代理是把自己的网络环境切换成代理的网络 反向代理是代理机器返回给我要我的资源 本文借鉴参考于http://tengine.taobao.org/book/chapter_02.html. 属于纯干 ...
- lua中.和:的区别
local myTable = {} function myTable:putMyname(val) print(val) print(self and self.name) end myTable. ...
- vue中子组件需调用父组件通过异步获取的数据
原因:子组件需要调用父组件传过来的数据,如果这个数据是异步从接口里取的,那这个组件在任何生命周期里都取不到,而应该在接口调取后取到. 需要在msg拿到值后才调用组件,然后你在生命周期created里面 ...
- Verilog之openMSP430(1)
openMSP430_IO interrupt Verilog file: omsp_gpio.v //================================================ ...
- CImage类的使用介绍!
链接参考:http://www.cnblogs.com/juncheng/articles/1600730.html CImage是MFC和ATL共享的新类,它能从外部磁盘中调入一个JPEG.GIF. ...
- 杭电2061WA 01
#include<stdio.h> struct mem { char s[50]; double c; double f; }; int main() { struct mem x[60 ...
- 04--奠定MYSQL江湖地位的开发注意要点
为不同的行业提供不同的MYSQL应用场景 吴炳锡老师谈到,不同行业的业务有不同的特点,选择好一个适合自己行业的MYSQL应用场景至关重要. 互联网行业 速度一直都是互联网发展的第一要义,互联网行业所使 ...