最近在使用MongoDB这个数据库,总结了有个比较全的工具类,该类是针对MongoDB封装的一个CRUD的工具类, 可以满足常规的数据查询, 数据写入, 数据修改, 数据删除操作。
刚接触MongoDB的小伙伴可以参考参考!!!!!!
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
public class MongoDao{
private static final Logger logger = LoggerFactory.getLogger(MongoDao.class);
public static MongoDatabase DB = null;
public MongoDao(String dbUrl, String dataBase) {
init(dbUrl, dataBase);
}
public MongoDao(String dbUrl, String dataBase, String username, String password) {
init(dbUrl, dataBase, username, password);
}
/**
* 无认证连接数据库
*
* @param String dbUrl (localhost:27017,host2:27017,host3:27017)
* @param String dataBase 数据库名称
*/
private void init(String dbUrl, String dataBase){
MongoClientOptions.Builder build = new MongoClientOptions.Builder();
build.connectionsPerHost(100); //最大连接数
build.threadsAllowedToBlockForConnectionMultiplier(50); //排队线程数
build.maxWaitTime(1000 * 60 * 2); //最大等待时间
build.connectTimeout(1000 * 60 * 10); //最长连接时间
MongoClientOptions mongoClientOptions = build.build();
String[] urlArr = dbUrl.split(",");
List<ServerAddress> addressList = new ArrayList<ServerAddress>();
for (String url : urlArr) {
ServerAddress serverAddress = new ServerAddress(url.split(":")[0], Integer.parseInt(url.split(":")[1]));
addressList.add(serverAddress);
}
//连接到 mongodb 服务
MongoClient mongoClient = new MongoClient(addressList, mongoClientOptions);
// ServerAddress serverAddress = new ServerAddress("localhost", 27017);
// MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://host1:27017,host2:27017,host3:27017"));
//连接到数据库
DB = mongoClient.getDatabase(dataBase);
}
/**
* 有认证连接数据库
*
* @param String dbUrl (localhost:27017,host2:27017,host3:27017)
* @param String dataBase 数据库名称
* @param String username 用户名
* @param String password 密码
*/
private void init(String dbUrl, String dataBase, String username, String password){
MongoClientOptions.Builder build = new MongoClientOptions.Builder();
build.connectionsPerHost(100); //最大连接数
build.threadsAllowedToBlockForConnectionMultiplier(50); //排队线程数
build.maxWaitTime(1000 * 60 * 2); //最大等待时间
build.connectTimeout(1000 * 60 * 10); //最长连接时间
MongoClientOptions mongoClientOptions = build.build();
String[] urlArr = dbUrl.split(",");
List<ServerAddress> addressList = new ArrayList<ServerAddress>();
for (String url : urlArr) {
ServerAddress serverAddress = new ServerAddress(url.split(":")[0], Integer.parseInt(url.split(":")[1]));
addressList.add(serverAddress);
}
List<MongoCredential> credentials = new ArrayList<>();
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
credentials.add(mongoCredential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addressList, credentials, mongoClientOptions);
//连接到数据库
DB = mongoClient.getDatabase("local");
}
/**
* 直接返回DB连接对象
*
* 适用现有方法无法满足查询时, 可自定义Query, insert, update, delete
* */
public static MongoDatabase getDB() {
return DB;
}
/**
* 根据id检索文档
*
* @param String table
* @param String id
* @return
* @throws Exception
*/
public Map<String, Object> queryByID(String table, Object id) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
BasicDBObject query = new BasicDBObject("_id", id);
// DBObject接口和BasicDBObject对象:表示一个具体的记录,BasicDBObject实现了DBObject,是key-value的数据结构,用起来和HashMap是基本一致的。
FindIterable<Document> iterable = collection.find(query);
Map<String, Object> map = new HashMap<String, Object>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
map.putAll(cursor.next());
}
logger.debug("检索ID完毕,db:{},table:{},id:{} ", DB.getName(), table, id);
return map;
}
/**
* 根据doc检索文档集合,当doc是空的时候检索全部
*
* @param String table
* @param BasicDBObject doc
* @return
* @throws Exception
*/
public List<Map<String, Object>> queryByDoc(String table, Document doc) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
FindIterable<Document> iterable = collection.find(doc);
/**
* 1. 获取迭代器FindIterable<Document> 2. 获取游标MongoCursor<Document> 3.通过游标遍历检索出的文档集合
* */
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
Map<String, Object> map = new HashMap<String, Object>();
map.putAll(cursor.next());
list.add(map);
}
logger.debug("检索doc完毕,db:{},table:{},doc:{} ", DB.getName(), table, doc.toJson());
return list;
}
/**
* 根据条件检索文档集合
*
* @param String table
* @param Bson doc
* @return
* @throws Exception
*/
public List<Map<String, Object>> queryByFilters(String table, Bson filter) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
FindIterable<Document> iterable = collection.find(filter);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
Map<String, Object> map = new HashMap<String, Object>();
map.putAll(cursor.next());
list.add(map);
}
return list;
}
/**
* 根据条件分页检索文档集合
*
* @param String table
* @param Bson doc
* @param int page 页数
* @param int pageSize 每页数量
* @return
* @throws Exception
*/
public List<Map<String, Object>> queryByFilters(String table, Bson filter, int page, int pageSize) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
FindIterable<Document> iterable = collection.find(filter).skip(page*pageSize).limit(pageSize);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
Map<String, Object> map = new HashMap<String, Object>();
map.putAll(cursor.next());
list.add(map);
}
return list;
}
/**
* 检索全部返回集合
*
* @param String table
* @return
* @throws Exception
*/
public List<Map<String, Object>> queryAll(String table) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
FindIterable<Document> iterable = collection.find();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
Map<String, Object> map = new HashMap<String, Object>();
map.putAll(cursor.next());
list.add(map);
}
logger.info("检索全部完毕,db:{},table:{}", DB.getName(), table);
return list;
}
/**
* 检索全部返回集合(分页)
*
* @param String table
* @param int page 页数
* @param int pageSize 每页数量
* @return
* @throws Exception
*/
public List<Map<String, Object>> queryAll(String table, int page, int pageSize) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
FindIterable<Document> iterable = collection.find().skip(page*pageSize).limit(pageSize);;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
Map<String, Object> map = new HashMap<String, Object>();
map.putAll(cursor.next());
list.add(map);
}
logger.info("检索全部完毕,db:{},table:{}", DB.getName(), table);
return list;
}
/**
* 聚合查询(推荐)
*
* @param String table
* @param BasicDBObject doc
* @return
* @throws Exception
*/
public List<Map<String, Object>> queryByAggregate(String table, List<Document> listDocument) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
AggregateIterable<Document> iterable = collection.aggregate(listDocument);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
Map<String, Object> map = new HashMap<String, Object>();
map.putAll(cursor.next());
list.add(map);
}
logger.debug("检索doc完毕,db:{},table:{},doc:{} ", DB.getName(), table, listDocument);
return list;
}
/**
* 遍历迭代器返回文档集合
*
* @param FindIterable iterable
* @return
* @throws Exception
*/
public List<Document> findIterable(FindIterable<Document> iterable) throws Exception {
List<Document> list = new ArrayList<Document>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
list.add(doc);
}
cursor.close();
return list;
}
/**
* 插入文档
*
* @param String table
* @param Document doc
* @return
* @throws Exception
*/
public boolean insert(String table, Document doc) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
collection.insertOne(doc);
long count = collection.count(doc);
if (count >= 1) {
logger.debug("文档插入成功,影响条数:{},db:{},table:{},doc:{} ", count, DB.getName(), table, doc.toJson());
return true;
} else {
logger.debug("文档插入失败,影响条数:{},db:{},table:{},doc:{} ", count, DB.getName(), table, doc.toJson());
return false;
}
}
/**
* 插入多条文档(推荐)
*
* @param String table
* @param List<Document> doc
* @param int batch
* @return
* @throws Exception
*/
public boolean insertMany(String table, List<Document> doc, int batch) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
long preCount = collection.count();
int insertCollectionSize = doc.size();
List<Document> insertList = new ArrayList<Document>();
for(int i=0; i<insertCollectionSize; i++) {
insertList.add(doc.get(i));
if(insertList.size()>0 && i%batch==0 && i!=0) {
collection.insertMany(insertList);
insertList.clear();
Thread.sleep(50);
}
}
if(insertList.size()>0) {
collection.insertMany(insertList);
insertList.clear();
}
long nowCount = collection.count();
if ((nowCount - preCount) == doc.size()) {
logger.debug("文档插入成功,影响条数:{},db:{},table:{}", doc.size(), DB.getName(), table);
return true;
} else {
logger.debug("文档插入失败,影响条数:{},db:{},table:{}", (nowCount - preCount), DB.getName(), table);
return false;
}
}
/**
* 批量保存或更新数据(存在时更新, 不存在时插入)
* 推荐使用
*
* @param String table
* @param String primaryKey 集合主键
* @param List<Document> docList
* @return
* @throws Exception
*/
public String saveOrUpdate(String table, String primaryKey, List<Document> docList) throws Exception {
String ret = "no params";
if(docList.size()>0) {
MongoCollection<Document> collection = DB.getCollection(table);
long preCount = collection.count();
long modifiedCount = 0;
long startTime = new Date().getTime();
for (Document document : docList) {
UpdateResult updateManyResult = collection.updateMany(Filters.eq(primaryKey, document.get(primaryKey)), new Document("$set", document), new UpdateOptions().upsert(true));
modifiedCount = updateManyResult.getModifiedCount() + modifiedCount;
}
long endTime = new Date().getTime();
long insCount = collection.count();
logger.debug("文档更新影响条数:{}", modifiedCount);
ret = "update num:[ " + modifiedCount + " ] " + " insert num:[ " + (insCount-preCount) + " ] spend time:[ " + (endTime-startTime) + "ms ]";
}
return ret;
}
/**
* 批量删除文档
*
* @param String table
* @param Bson bson Filters.eq("_id", new ArrayList(new ObjectId("5de156a2dbcf3529a36724bc"), new ObjectId("5de156a2dbcf3529a36724bc"))))
* @return
* @throws Exception
*/
public String deleteMany(String table, Bson bson) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
DeleteResult deleteManyResult = collection.deleteMany(bson);
long deletedCount = deleteManyResult.getDeletedCount();
if (deletedCount > 0) {
logger.debug("文档删除成功,影响条数:{},db:{},table:{},doc:{} ", deletedCount, DB.getName(), table, bson);
return "Delete Num:[ " + deletedCount + " ]";
} else {
logger.debug("文档删除失败,影响条数:{},db:{},table:{},doc:{} ", 0, DB.getName(), table, bson);
return "Delete Num:[ 0 ]";
}
}
/**
* 删除单条文档
*
* @param String table
* @param Bson bson Filters.eq("_id", new ObjectId("5de156a2dbcf3529a36724bc")))
* @return
* @throws Exception
*/
public boolean deleteOne(String table, Bson bson) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
DeleteResult deleteOneResult = collection.deleteOne(bson);
long deletedCount = deleteOneResult.getDeletedCount();
System.out.println("删除的数量: " + deletedCount);
if (deletedCount == 1) {
logger.debug("文档删除成功,影响条数:{},db:{},table:{},doc:{} ", deletedCount, DB.getName(), table, bson);
return true;
} else {
logger.debug("文档删除失败,影响条数:{},db:{},table:{},doc:{} ", 0, DB.getName(), table, bson);
return false;
}
}
/**
* 修改文档(文档不存在时, insert)
*
* @param String table
* @param BasicDBObject oldDoc
* @param BasicDBObject newDoc
* @return
* @throws Exception
*/
public String updateManny(String table, Document whereDoc, Document updateDoc) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
UpdateResult updateManyResult = collection.updateMany(whereDoc, new Document("$set", updateDoc), new UpdateOptions().upsert(true));
long modifiedCount = updateManyResult.getModifiedCount();
System.out.println("修改的数量: " + modifiedCount);
if (modifiedCount > 0) {
logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", modifiedCount, DB.getName(), table, whereDoc.toJson(), updateDoc.toJson());
return "Update Num:[ " + modifiedCount + " ]";
} else {
logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 0, DB.getName(), table, whereDoc.toJson(), updateDoc.toJson());
return "Update Num:[ 0 ]";
}
}
/**
* 修改文档(文档不存在时, insert)
*
* @param String table
* @param BasicDBObject oldDoc
* @param BasicDBObject newDoc
* @return
* @throws Exception
*/
public String updateManny(String table, Bson bson, Document updateDoc) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
UpdateResult updateManyResult = collection.updateMany(bson, new Document("$set", updateDoc), new UpdateOptions().upsert(true));
long modifiedCount = updateManyResult.getModifiedCount();
System.out.println("修改的数量: " + modifiedCount);
if (modifiedCount > 0) {
logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", modifiedCount, DB.getName(), table, bson, updateDoc.toJson());
return "Update Num:[ " + modifiedCount + " ]";
} else {
logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 0, DB.getName(), table, bson, updateDoc.toJson());
return "Update Num:[ 0 ]";
}
}
/**
* 修改单条文档(文档不存在时, insert)
*
* @param String table
* @param BasicDBObject whereDoc
* @param BasicDBObject updateDoc
* @return
* @throws Exception
*/
public boolean updateOne(String table, Document whereDoc, Document updateDoc) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
UpdateResult updateOneResult = collection.updateOne(whereDoc, new Document("$set", updateDoc), new UpdateOptions().upsert(true));
long modifiedCount = updateOneResult.getModifiedCount();
System.out.println("修改的数量: " + modifiedCount);
if (modifiedCount == 1) {
logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 1, DB.getName(), table, whereDoc.toJson(), updateDoc.toJson());
return true;
} else {
logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 0, DB.getName(), table, whereDoc.toJson(), updateDoc.toJson());
return false;
}
}
/**
* 修改单条文档(文档不存在时, insert)
*
* @param String table
* @param BasicDBObject whereDoc
* @param BasicDBObject updateDoc
* @return
* @throws Exception
*/
public boolean updateOne(String table, Bson bson, Document updateDoc) throws Exception {
MongoCollection<Document> collection = DB.getCollection(table);
UpdateResult updateOneResult = collection.updateOne(bson, new Document("$set", updateDoc), new UpdateOptions().upsert(true));
long modifiedCount = updateOneResult.getModifiedCount();
System.out.println("修改的数量: " + modifiedCount);
if (modifiedCount == 1) {
logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 1, DB.getName(), table, bson, updateDoc.toJson());
return true;
} else {
logger.debug("文档更新成功,影响条数:{},db:{},table:{},whereDoc:{},updateDoc:{} ", 0, DB.getName(), table, bson, updateDoc.toJson());
return false;
}
}
/**
* 创建集合
*
* @param table
* @throws Exception
*/
public void createCol(String table) throws Exception {
DB.createCollection(table);
logger.debug("集合创建成功,db:{},table:{}", DB.getName(), table);
}
/**
* 删除集合
*
* @param table
* @throws Exception
*/
public void dropCol(String table) throws Exception {
DB.getCollection(table).drop();
logger.debug("集合删除成功,db:{},table:{}", DB.getName(), table);
}
}
以上是工具类,接下来是测试类
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.Test;
import com.mongodb.client.model.Filters;
import com.uinnova.di.dicom.util.MongoDao;
public class MongoDBTest {
MongoDao DBUTIL = new MongoDao("192.168.1.1:27017", "local");
@Test
public void queryByID() throws Exception {
Map<String, Object> ret = DBUTIL.queryByID("dix", "hhh0");
System.out.println(ret);
}
@Test
public void queryByDoc() throws Exception {
Document doc = new Document();
doc.put("_id", "hhh1");
List<Map<String, Object>> ret = DBUTIL.queryByDoc("dix", doc);
System.out.println(ret);
}
@Test
public void queryByFilters() throws Exception {
List<Map<String, Object>> ret = DBUTIL.queryByFilters("dix", Filters.or(Filters.lte("_id",4),Filters.eq("name","hhh3")));
System.out.println(ret);
List<Map<String, Object>> ret1 = DBUTIL.queryByFilters("dix", Filters.in("name", "hhh1", "hhh2", "hhh3"));
System.out.println(ret1);
}
@Test
public void queryByFiltersByPage() throws Exception {
List<Map<String, Object>> ret = DBUTIL.queryByFilters("dix", Filters.or(Filters.lte("_id",4),Filters.eq("name","hhh3")), 0, 200);
System.out.println(ret);
List<Map<String, Object>> ret1 = DBUTIL.queryByFilters("dix", Filters.in("name", "hhh1", "hhh2", "hhh3"), 0, 200);
System.out.println(ret1);
}
@Test
public void queryAll() throws Exception {
List<Map<String, Object>> ret = DBUTIL.queryAll("dix");
System.out.println(ret);
}
@Test
public void queryAllBypage() throws Exception {
List<Map<String, Object>> ret = DBUTIL.queryAll("dix", 1, 10);
System.out.println(ret);
}
@Test
public void queryByAggregate() throws Exception {
List<Document> list = new ArrayList<>();
Document match = new Document("name","hhh3");
Document doc1 = new Document();
doc1.put("$match", match);
list.add(doc1);
List<Map<String, Object>> ret = DBUTIL.queryByAggregate("dix", list);
System.out.println(ret);
}
@Test
public void insert() throws Exception {
Document doc = new Document();
doc.put("name", "hello");
doc.put("age", 12);
boolean ret = DBUTIL.insert("dix", doc);
System.out.println(ret);
}
@Test
public void insertMany() throws Exception {
List<Document> insertList = new ArrayList<Document>();
for(int i = 0; i<50; i++) {
Document a = new Document();
a.put("name", "hhh" + i);
a.put("age", i);
insertList.add(a);
}
boolean ret = DBUTIL.insertMany("dix", insertList, 20);
System.out.println(ret);
}
@Test
public void saveOrUpdate() throws Exception {
List<Document> insertList = new ArrayList<Document>();
for(int i = 0; i<2000; i++) {
Document a = new Document();
a.put("_id", "hh" + i);
a.put("name", "f" + i);
a.put("age", i*2);
a.put("sdf", "dsdf" + i);
a.put("asdff", i*2);
insertList.add(a);
}
String ret = DBUTIL.saveOrUpdate("dix", "_id",insertList);
System.out.println(ret);
}
@Test
public void deleteOne() throws Exception {
ObjectId id = new ObjectId("5de24b95b01a4e6b3ed09889");
boolean ret = DBUTIL.deleteOne("dix", Filters.eq("_id", id));
System.out.println(ret);
}
@Test
public void deleteMany() throws Exception {
List<ObjectId> del = new ArrayList<ObjectId>();
del.add(new ObjectId("5de156a2dbcf3529a36724bc"));
del.add(new ObjectId("5de2162cfcf51c0f56b6eaeb"));
del.add(new ObjectId("5de228fa8c0f613fe77a5d9a"));
String ret = DBUTIL.deleteMany("dix", Filters.eq("_id", del));
System.out.println(ret);
}
@Test
public void updateOne() throws Exception {
Document old = new Document();
old.put("name", "asdf");
Document upd = new Document();
upd.put("name", "777");
boolean ret = DBUTIL.updateOne("dix", old, upd);
System.out.println(ret);
boolean ret1 = DBUTIL.updateOne("dix", Filters.eq("name", "6666"), upd);
System.out.println(ret1);
}
@Test
public void updateMany() throws Exception {
Document upd = new Document();
upd.put("name", "777");
String ret1 = DBUTIL.updateManny("dix", Filters.in("name", "hhh1", "hhh2", "hhh3"), upd);
System.out.println(ret1);
}
@Test
public void cus() throws Exception{
System.out.println(DBUTIL.getDB());
}
}
以上就是今天的内容啦,大家一起学习,一起进去,争取早日成为大佬!!!!