MongoDB Java 通用方法

package model.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
* MongoDB Manager
* @author Ken Chau
*
*/
public class MongoDBManager {
     private static Logger log = Logger.getLogger(MongoDBManager.class);
    
     private static Mongo mg = null;
     private static DB db = null;
    
     private final static MongoDBManager instance = new MongoDBManager();
    
     /**
     * 实例化
     * @return
     * @throws Exception
     */
     public static MongoDBManager getInstance() throws Exception {
            return instance;
     }
    
     static {
          try {
               mg = new Mongo(HOST, PORT);
              db=mg.getDB(DB);
          } catch (Exception e) {
               log.error("Can't connect MongoDB!");
              e.printStackTrace();
          }
     }
           
     /**
     * 获取集合(表)
     * @param collection
     */
     public static DBCollection getCollection(String collection) {
          return db.getCollection(collection);
     }

     /**
     * ----------------------------------分割线--------------------------------------
     */

     /**
     * 插入
     * @param collection
     * @param map
     */
     public void insert(String collection , Map<String, Object> map) {
          try {
               DBObject dbObject = map2Obj(map);
               getCollection(collection).insert(dbObject);
          } catch (MongoException e) {
               log.error("MongoException:" + e.getMessage());
          }
     }

     /**
     * 批量插入
     * @param collection
     * @param list
     */
     public void insertBatch(String collection ,List<Map<String, Object>> list) {
          if (list == null || list.isEmpty()) {
               return;
          }
          try {
               List<DBObject> listDB = new ArrayList<DBObject>();
               for (int i = 0; i < list.size(); i++) {
                    DBObject dbObject = map2Obj(list.get(i));
                    listDB.add(dbObject);
               }
               getCollection(collection).insert(listDB);
          } catch (MongoException e) {
               log.error("MongoException:" + e.getMessage());
          }
     }

     /**
     * 删除
     * @param collection
     * @param map
     */
     public void delete(String collection ,Map<String, Object> map) {
          DBObject obj = map2Obj(map);
          getCollection(collection).remove(obj);
     }

     /**
       * 删除全部
       * @param collection
       * @param map
       */
     public void deleteAll(String collection) {
          List<DBObject> rs = findAll(collection);
          if (rs != null && !rs.isEmpty()) {
               for (int i = 0; i < rs.size(); i++) {
                    getCollection(collection).remove(rs.get(i));
               }
          }
     }

     /**
     * 批量删除
     * @param collection
     * @param list
     */
     public void deleteBatch(String collection,List<Map<String, Object>> list) {
          if (list == null || list.isEmpty()) {
               return;
          }
          for (int i = 0; i < list.size(); i++) {
               getCollection(collection).remove(map2Obj(list.get(i)));
          }
     }

     /**
     * 计算满足条件条数
     * @param collection
     * @param map
     */
     public long getCount(String collection,Map<String, Object> map) {
          return getCollection(collection).getCount(map2Obj(map));
     }
    
     /**
     * 计算集合总条数
     * @param collection
     * @param map
     */
     public long getCount(String collection) {
          return getCollection(collection).find().count();
     }

     /**
     * 更新
     * @param collection
     * @param setFields
     * @param whereFields
     */
     public void update(String collection,Map<String, Object> setFields,
               Map<String, Object> whereFields) {
          DBObject obj1 = map2Obj(setFields);
          DBObject obj2 = map2Obj(whereFields);
          getCollection(collection).updateMulti(obj1, obj2);
     }

     /**
     * 查找对象(根据主键_id)
     * @param collection
     * @param _id
     */
     public DBObject findById(String collection,String _id) {
          DBObject obj = new BasicDBObject();
          obj.put("_id", ObjectId.massageToObjectId(_id));
          return getCollection(collection).findOne(obj);
     }

     /**
     * 查找集合所有对象
     * @param collection
     */
     public List<DBObject> findAll(String collection) {
          return getCollection(collection).find().toArray();
     }

     /**
     * 查找(返回一个对象)
     * @param map
     * @param collection
     */
     public DBObject findOne(String collection,Map<String, Object> map) {
          DBCollection coll = getCollection(collection);
          return coll.findOne(map2Obj(map));
     }

     /**
     * 查找(返回一个List<DBObject>)
     * @param <DBObject>
     * @param map
     * @param collection
     * @throws Exception
     */
     public List<DBObject> find(String collection,Map<String, Object> map) throws Exception {
          DBCollection coll = getCollection(collection);
          DBCursor c = coll.find(map2Obj(map));
          if (c != null)
               return c.toArray();
          else
               return null;
     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值