http://blog.csdn.net/phinecos/article/details/8444515
点击(此处)折叠或打开
- package cls;
- import java.net.UnknownHostException;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import org.slf4j.LoggerFactory;
- import org.slf4j.Logger;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.Mongo;
- import com.mongodb.WriteResult;
- public class MongoDbHelper {
- protected static Logger logger = LoggerFactory.getLogger("MongoDbHelper");
- private Mongo myMongo = null;
- private DB myDb = null;
- private static final String serverAddr="127.0.0.1";
- private static final int port=27017;
- private static final String myDbName="test";
- private static final String userName="admin";
- private static final String passWord="123456";
- private static Map<String, DBCollection> dbCollectionMap = new ConcurrentHashMap<String, DBCollection>();
-
- public MongoDbHelper(){
- this.init();
- }
- private void init() {
- if (this.myMongo == null) {
- try {
- this.myMongo = new Mongo( MongoDbHelper.serverAddr , MongoDbHelper.port);
- if (null != this.myMongo) {
- this.myDb = this.myMongo.getDB(MongoDbHelper.myDbName);
- //如果需要用户认证则先切换到admin数据库进行用户认证后,再切回用户数据库;
- if(userName!=""){
- this.myDb=this.myMongo.getDB("admin");
- this.myDb.authenticate(userName, passWord.toCharArray());
- this.myDb=this.myMongo.getDB(MongoDbHelper.myDbName);
- }
- }
- } catch (UnknownHostException e) {
- logger.error("连接mongoDb失败, 服务器地址: " + MongoDbHelper.serverAddr + ", 端口: " + MongoDbHelper.port);
- throw new RuntimeException(e);
- }
- }
- }
- private DBCollection getDBCollection(String collectionName) {
- DBCollection collection = null;
- if (dbCollectionMap.containsKey(collectionName)) {
- collection = dbCollectionMap.get(collectionName);
- } else {
- collection = this.myDb.getCollection(collectionName);
- if (null != collection) {
- dbCollectionMap.put(collectionName, collection);
- }
- }
- return collection;
- }
-
- public boolean isDocumentExsit(String collectionName, DBObject query) {
- boolean result = false;
- DBCursor dbCursor = null;
- DBCollection collection = this.getDBCollection(collectionName);
- if (null != collection) {
- dbCursor = collection.find(query);
- if (null != dbCursor && dbCursor.hasNext()) {
- result = true;
- }
- }
- return result;
- }
-
- public DBCursor selectDocument(String collectionName, DBObject query) {
- DBCursor result = null;
- DBCursor dbCursor = null;
- DBCollection collection = this.getDBCollection(collectionName);
- if (null != collection) {
- dbCursor = collection.find(query);
- if (null != dbCursor && dbCursor.hasNext()) {
- result = dbCursor;
- }
- }
- return result;
- }
-
- public void insertDocument(String collectionName, DBObject newDocument) {
- DBCollection collection = this.getDBCollection(collectionName);
- if (null != collection) {
- if (!this.isDocumentExsit(collectionName, newDocument)) {//insert only doc not exist
- collection.insert(newDocument);
- }
- }
- }
-
- public boolean updateDocument(String collectionName, DBObject query, DBObject updatedDocument) {
- boolean result = false;
- WriteResult writeResult = null;
- DBCollection collection = this.getDBCollection(collectionName);
- if (null != collection) {
- writeResult = collection.update(query, updatedDocument);
- if (null != writeResult) {
- if (writeResult.getN() > 0) {
- result = true;
- }
- }
- }
- return result;
- }
- //以下代码为测试mongodb的save功能
- /*
- DBObject upObject =new BasicDBObject("_id", new ObjectId("52fdbe36e97fee8601000000"));
- upObject.put("enterpriseName","cccc");
- myMongoDb.saveDoc("testColl", upObject);*/
- //============================================
- public boolean saveDoc(String collectionName,DBObject saveObject){
- boolean result=false;
- WriteResult writeResult = null;
- DBCollection collection = this.getDBCollection(collectionName);
- if (null != collection) {
- writeResult = collection.save(saveObject);
- if (null != writeResult) {
- if (writeResult.getN() > 0) {
- result = true;
- }
- }
- }
- return result;
- }
-
- public boolean deleteDocument(String collectionName, DBObject query) {
- boolean result = false;
- WriteResult writeResult = null;
- DBCollection collection = this.getDBCollection(collectionName);
- if (null != collection) {
- writeResult = collection.remove(query);
- if (null != writeResult) {
- if (writeResult.getN() > 0) {
- result = true;
- }
- }
- }
- return result;
- }
-
-
- }