日期:2014-05-16 浏览次数:20662 次
MongoDB的一些常用操作的公共类:
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)));
????????? }
???? }