java mongodb单例_JAVA单例MongoDB工具类

// ------------------------------------共用方法---------------------------------------------------

/**

* 获取DB实例 - 指定DB

*

* @param dbName

* @return

*/

public MongoDatabase getDB(String dbName) {

if (dbName != null && !"".equals(dbName)) {

MongoDatabase database = mongoClient.getDatabase(dbName);

return database;

}

return null;

}

/**

* 获取collection对象 - 指定Collection

*

* @param collName

* @return

*/

public MongoCollection getCollection(String dbName, String collName) {

if (null == collName || "".equals(collName)) {

return null;

}

if (null == dbName || "".equals(dbName)) {

return null;

}

MongoCollection collection = mongoClient.getDatabase(dbName).getCollection(collName);

return collection;

}

/**

* 查询DB下的所有表名

*/

public List getAllCollections(String dbName) {

MongoIterable colls = getDB(dbName).listCollectionNames();

List _list = new ArrayList();

for (String s : colls) {

_list.add(s);

}

return _list;

}

/**

* 获取所有数据库名称列表

*

* @return

*/

public MongoIterable getAllDBNames() {

MongoIterable s = mongoClient.listDatabaseNames();

return s;

}

/**

* 删除一个数据库

*/

public void dropDB(String dbName) {

getDB(dbName).drop();

}

/**

* 查找对象 - 根据主键_id

*

* @param collection

* @param id

* @return

*/

public Document findById(MongoCollection coll, String id) {

ObjectId _idobj = null;

try {

_idobj = new ObjectId(id);

} catch (Exception e) {

return null;

}

Document myDoc = coll.find(Filters.eq("_id", _idobj)).first();

return myDoc;

}

/** 统计数 */

public int getCount(MongoCollection coll) {

int count = (int) coll.count();

return count;

}

/** 条件查询 */

public MongoCursor find(MongoCollection coll, Bson filter) {

return coll.find(filter).iterator();

}

/** 分页查询 */

public MongoCursor findByPage(MongoCollection coll, Bson filter, int pageNo, int pageSize) {

Bson orderBy = new BasicDBObject("_id", 1);

return coll.find(filter).sort(orderBy).skip((pageNo - 1) * pageSize).limit(pageSize).iterator();

}

/**

* 通过ID删除

*

* @param coll

* @param id

* @return

*/

public int deleteById(MongoCollection coll, String id) {

int count = 0;

ObjectId _id = null;

try {

_id = new ObjectId(id);

} catch (Exception e) {

return 0;

}

Bson filter = Filters.eq("_id", _id);

DeleteResult deleteResult = coll.deleteOne(filter);

count = (int) deleteResult.getDeletedCount();

return count;

}

/**

* FIXME

*

* @param coll

* @param id

* @param newdoc

* @return

*/

public Document updateById(MongoCollection coll, String id, Document newdoc) {

ObjectId _idobj = null;

try {

_idobj = new ObjectId(id);

} catch (Exception e) {

return null;

}

Bson filter = Filters.eq("_id", _idobj);

// coll.replaceOne(filter, newdoc); // 完全替代

coll.updateOne(filter, new Document("$set", newdoc));

return newdoc;

}

public void dropCollection(String dbName, String collName) {

getDB(dbName).getCollection(collName).drop();

}

/**

* 关闭Mongodb

*/

public void close() {

if (mongoClient != null) {

mongoClient.close();

mongoClient = null;

}

}

/**

* 测试入口

*

* @param args

*/

public static void main(String[] args) {

String dbName = "GC_MAP_DISPLAY_DB";

String collName = "COMMUNITY_BJ";

MongoCollection coll = MongoDBUtil.instance.getCollection(dbName, collName);

// 插入多条

// for (int i = 1; i <= 4; i++) {

// Document doc = new Document();

// doc.put("name", "zhoulf");

// doc.put("school", "NEFU" + i);

// Document interests = new Document();

// interests.put("game", "game" + i);

// interests.put("ball", "ball" + i);

// doc.put("interests", interests);

// coll.insertOne(doc);

// }

// // 根据ID查询

// String id = "556925f34711371df0ddfd4b";

// Document doc = MongoDBUtil2.instance.findById(coll, id);

// System.out.println(doc);

// 查询多个

// MongoCursor cursor1 = coll.find(Filters.eq("name", "zhoulf")).iterator();

// while (cursor1.hasNext()) {

// org.bson.Document _doc = (Document) cursor1.next();

// System.out.println(_doc.toString());

// }

// cursor1.close();

// 查询多个

// MongoCursor cursor2 = coll.find(Person.class).iterator();

// 删除数据库

// MongoDBUtil2.instance.dropDB("testdb");

// 删除表

// MongoDBUtil2.instance.dropCollection(dbName, collName);

// 修改数据

// String id = "556949504711371c60601b5a";

// Document newdoc = new Document();

// newdoc.put("name", "时候");

// MongoDBUtil.instance.updateById(coll, id, newdoc);

// 统计表

// System.out.println(MongoDBUtil.instance.getCount(coll));

// 查询所有

Bson filter = Filters.eq("count", 0);

MongoDBUtil.instance.find(coll, filter);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值