標籤:io os ar 使用 java for on cti ad
package com.x.mongodb; import java.net.UnknownHostException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.bson.types.ObjectId; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.MongoException; public final class MongoDBUtil { private static final String HOST = "127.0.0.1"; private static final String dbName = "test"; private static Mongo mongo; private static DB db; static { try { mongo = new Mongo(HOST); db = mongo.getDB(dbName); // db.authenticate(username, passwd) } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } private MongoDBUtil() { } /** * 添加操作 * <br>------------------------------<br> * @param map * @param collectionName */ public static void add(Map<String, Object> map, String collectionName) { DBObject dbObject = new BasicDBObject(map); getCollection(collectionName).insert(dbObject); } /** * 添加操作 * <br>------------------------------<br> * @param list * @param collectionName */ public static void add(List<Map<String, Object>> list, String collectionName) { for (Map<String, Object> map : list) { add(map, collectionName); } } /** * 刪除操作 * <br>------------------------------<br> * @param map * @param collectionName */ public static void delete(Map<String, Object> map, String collectionName) { DBObject dbObject = new BasicDBObject(map); getCollection(collectionName).remove(dbObject); } /** * 刪除操作,根據主鍵 * <br>------------------------------<br> * @param id * @param collectionName */ public static void delete(String id, String collectionName) { Map<String, Object> map = new HashMap<String, Object>(); map.put("_id", new ObjectId(id)); delete(map, collectionName); } /** * 刪除全部 * <br>------------------------------<br> * @param collectionName */ public static void deleteAll(String collectionName) { getCollection(collectionName).drop(); } /** * 修改操作</br> * 會用一個新文檔替換現有文檔,文檔key結構會發生改變</br> * 比如原文檔{"_id":"123","name":"zhangsan","age":12}當根據_id修改age * value為{"age":12}建立的文檔name值會沒有,結構發生了改變 * <br>------------------------------<br> * @param whereMap * @param valueMap * @param collectionName */ public static void update(Map<String, Object> whereMap, Map<String, Object> valueMap, String collectionName) { executeUpdate(collectionName, whereMap, valueMap, new UpdateCallback(){ public DBObject doCallback(DBObject valueDBObject) { return valueDBObject; } }); } /** * 修改操作,使用$set修改器</br> * 用來指定一個索引值,如果鍵不存在,則自動建立,會更新原來文檔, 不會產生新的, 結構不會發生改變 * <br>------------------------------<br> * @param whereMap * @param valueMap * @param collectionName */ public static void updateSet(Map<String, Object> whereMap, Map<String, Object> valueMap, String collectionName) { executeUpdate(collectionName, whereMap, valueMap, new UpdateCallback(){ public DBObject doCallback(DBObject valueDBObject) { return new BasicDBObject("$set", valueDBObject); } }); } /** * 修改操作,使用$inc修改器</br> * 修改器鍵的值必須為數字</br> * 如果鍵存在增加或減少鍵的值, 如果不存在建立鍵 * <br>------------------------------<br> * @param whereMap * @param valueMap * @param collectionName */ public static void updateInc(Map<String, Object> whereMap, Map<String, Integer> valueMap, String collectionName) { executeUpdate(collectionName, whereMap, valueMap, new UpdateCallback(){ public DBObject doCallback(DBObject valueDBObject) { return new BasicDBObject("$inc", valueDBObject); } }); } /** * 修改 * <br>------------------------------<br> * @param collectionName * @param whereMap * @param valueMap * @param updateCallback */ private static void executeUpdate(String collectionName, Map whereMap, Map valueMap, UpdateCallback updateCallback) { DBObject whereDBObject = new BasicDBObject(whereMap); DBObject valueDBObject = new BasicDBObject(valueMap); valueDBObject = updateCallback.doCallback(valueDBObject); getCollection(collectionName).update(whereDBObject, valueDBObject); } interface UpdateCallback { DBObject doCallback(DBObject valueDBObject); } /** * 擷取集合(表) * <br>------------------------------<br> * @param collectionName * @return */ public static DBCollection getCollection(String collectionName) { return db.getCollection(collectionName); } }
mongodb- Java API 增刪改操作