-
.limit(mongoDBCursor.getLimit());
-
}
-
if (mongoDBCursor.getSortObject() != null) {
-
cursorToUse = cursorToUse.sort(mongoDBCursor.getSortObject());
-
}
-
return cursorToUse;
-
}
-
};
-
return find(mongoDBCursor, dbObject, cursorPreparer,customField);
-
}
-
/**
-
* 查詢(私有方法,真正的查詢操作)
-
*
-
* @param query 查詢條件
-
* @param mongoDBCursor 查詢實體
-
* @param cursorPreparer 查詢轉換接口
-
*/
-
private static List<DBObject> find(MongoDBCursor mongoDBCursor, DBObject query, MongoDBCursorPreparer cursorPreparer,BasicDBObjectBuilder customField) {
-
DBCursor dbCursor = null;
-
if(customField == null) {
-
dbCursor = MongoDBCommonUtil.getCollection(mongoDBCursor).find(query);
-
} else {
-
dbCursor = MongoDBCommonUtil.getCollection(mongoDBCursor).find(query,customField.get());
-
}
-
if (cursorPreparer != null) {
-
dbCursor = cursorPreparer.prepare(dbCursor);
-
}
-
return dbCursor.toArray();
-
}
-
/**
-
* Count查詢
-
*
-
* @param mongoDBCursor 查詢實體
-
* @return 總數
-
*/
-
public static long count(MongoDBCursor mongoDBCursor) {
-
DBObject dbObject = getMapped(mongoDBCursor.getFieldMap());
-
return MongoDBCommonUtil.getCollection(mongoDBCursor).count(dbObject);
-
}
-
/**
-
* 把參數Map轉換DBObject
-
*
-
* @param map 查詢條件
-
* @return DBObject
-
*/
-
private static DBObject getMapped(Map<String, Object> map) {
-
DBObject dbObject = new BasicDBObject();
-
Iterator<Map.Entry<String, Object>> iterable = map.entrySet().iterator();
-
while (iterable.hasNext()) {
-
Map.Entry<String, Object> entry = iterable.next();
-
Object value = entry.getValue();
-
String key = entry.getKey();
-
if (key.startsWith("$") && value instanceof Map) {
-
BasicBSONList basicBSONList = new BasicBSONList();
-
Map<String, Object> conditionsMap = ((Map) value);
-
// Set<String> keys = conditionsMap.keySet();
-
for (String k : conditionsMap.keySet()) {
-
Object conditionsValue = conditionsMap.get(k);
-
if (conditionsValue instanceof Collection) {
-
conditionsValue = convertArray(conditionsValue);
-
}
-
DBObject dbObject2 = new BasicDBObject(k, conditionsValue);
-
basicBSONList.add(dbObject2);
-
}
-
value = basicBSONList;
-
} else if (value instanceof Collection) {
-
value = convertArray(value);
-
} else if (!key.startsWith("$") && value instanceof Map) {
-
value = getMapped(((Map) value));
-
}
-
dbObject.put(key, value);
-
}
-
return dbObject;
-
}
-
/**
-
* 轉換成Object[]
-
*
-
* @param value 待轉換實體
-
* @return Object[]
-
*/
-
private static Object[] convertArray(Object value) {
-
Object[] values = ((Collection) value).toArray();
-
return values;
-
}
-
/**
-
* 添加操作
-
*
-
* @param mongoDBEntity 實體
-
*/
-
public static void add(MongoDBEntity mongoDBEntity) {
-
DBObject dbObject = new BasicDBObject(mongoDBEntity.getFieldMap());
-
MongoDBCommonUtil.getCollection(mongoDBEntity).insert(dbObject);
-
}
-
/**
-
* 批量處理添加操作
-
*
-
* @param list 批量字段數據
-
* @param mongoDBEntity 實體
-
*/
-
public static void add(MongoDBEntity mongoDBEntity, List<Map<String, Object>> list) {
-
for (Map<String, Object> map : list) {
-
mongoDBEntity.setFieldMap(map);
-
add(mongoDBEntity);
-
}
-
}
-
/**
-
* 刪除操作
-
*
-
* @param mongoDBEntity 實體
-
*/
-
public static void delete(MongoDBEntity mongoDBEntity) {
-
DBObject dbObject = new BasicDBObject(mongoDBEntity.getFieldMap());
-
MongoDBCommonUtil.getCollection(mongoDBEntity).remove(dbObject);
-
}
-
/**
-
* 刪除操作,根據主鍵
-
*
-
* @param id 主鍵
-
* @param mongoDBEntity 實體
-
*/
-
public static void delete(MongoDBEntity mongoDBEntity, String id) {
-
Map<String, Object> map = new HashMap<String, Object>();
-
map.put("_id", new ObjectId(id));
-
mongoDBEntity.setFieldMap(map);
-
delete(mongoDBEntity);
-
}
-
/**
-
* 刪除全部
-
*
-
* @param mongoDBEntity 實體
-
*/
-
public static void deleteAll(MongoDBEntity mongoDBEntity) {
-
MongoDBCommonUtil.getCollection(mongoDBEntity).drop();
-
}
-
/**
-
* 修改操作
-
* 會用一個新文檔替換現有文檔,文檔key結構會發生改變
-
* 比如原文檔{"_id":"123","name":"zhangsan","age":12}當根據_id修改age
-
* value爲{"age":12}新建的文檔name值會沒有,結構發生了改變
-
*
-
* @param mongoDBUpdate 更新實體
-
*/
-
public static void update(MongoDBUpdate mongoDBUpdate) {
-
executeUpdate(mongoDBUpdate, new UpdateCallback() {
-
public DBObject doCallback(DBObject valueDBObject) {
-
return valueDBObject;
-
}
-
});
-
}
-
/**
-
* 修改操作,使用$set修改器
-
* 用來指定一個鍵值,如果鍵不存在,則自動創建,會更新原來文檔, 不會生成新的, 結構不會發生改變
-
*
-
* @param mongoDBUpdate 更新實體
-
*/
-
public static void updateSet(MongoDBUpdate mongoDBUpdate) {
-
executeUpdate(mongoDBUpdate, new UpdateCallback() {
-
public DBObject doCallback(DBObject valueDBObject) {
-
return new BasicDBObject("$set", valueDBObject);
-
}
-
});
-
}
-
/**
-
* 修改操作,使用$inc修改器
-
* 修改器鍵的值必須爲數字
-
* 如果鍵存在增加或減少鍵的值, 如果不存在創建鍵
-
*
-
* @param mongoDBUpdate 更新實體
-
*/
-
public static void updateInc(MongoDBUpdate mongoDBUpdate) {
-
executeUpdate(mongoDBUpdate, new UpdateCallback() {
-
public DBObject doCallback(DBObject valueDBObject) {
-
return new BasicDBObject("$inc", valueDBObject);
-
}
-
});
-
}
-
/**
-
* 修改(私有方法)
-
*
-
* @param mongoDBUpdate 更新實體
-
* @param updateCallback 更新回調
-
*/
-
private static void executeUpdate(MongoDBUpdate mongoDBUpdate, UpdateCallback updateCallback) {
-
DBObject whereDBObject = new BasicDBObject(mongoDBUpdate.getWhereMap());
-
DBObject valueDBObject = new BasicDBObject(mongoDBUpdate.getValueMap());
-
valueDBObject = updateCallback.doCallback(valueDBObject);
-
MongoDBCommonUtil.getCollection(mongoDBUpdate).update(whereDBObject, valueDBObject);
-
}
-
public static void main(String[] args) {
-
try {
-
//獲取操作DB
-
DB db = MongoDBCommonUtil.getDB("192.168.227.170", 20011,"lagd","lagd_rw","lagd_pwd");
-
MongoDBCursor mongoDBCursor = new MongoDBCursor();
-
mongoDBCursor.setDb(db); //賦值DB
-
mongoDBCursor.setCollectionName("lagd_data_dictionary"); //賦值集合名
-
//封裝查詢條件
-
Map<String, Object> fieldMap = new HashMap<String, Object>();
-
fieldMap.put("type","dataSource");
-
mongoDBCursor.setFieldMap(fieldMap);
-
//賦值skip
-
mongoDBCursor.setSkip(1);
-
//賦值limit
-
mongoDBCursor.setLimit(1);
-
//封裝Sort
-
Map<String, Object> sortMap = new LinkedHashMap<String, Object>();
-
sortMap.put("key",1);
-
mongoDBCursor.setSort(sortMap);
-
//自定義查詢字段
-
Map<String, Object> customFieldMap = new LinkedHashMap<String, Object>();
-
customFieldMap.put("type","1");
-
customFieldMap.put("key","1");
-
customFieldMap.put("value","1");
-
mongoDBCursor.setCustomFieldMap(customFieldMap);
-
//查詢
-
List<DBObject> result = MongoDBUtil.find(mongoDBCursor);
-
for(DBObject dbObject : result){
-
for(String key : dbObject.keySet()){
-
System.out.println("鍵:" + key + "; 值:" + dbObject.get(key));
-
}
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
}
MongoDBCursorPreparer
-
import com.mongodb.DBCursor;
-
/**
-
* 查詢轉換接口定義
-
*
-
* @author: alex
-
* @time: 14-1-21 下午4:55
-
* @version: 1.0
-
*/
-
public interface MongoDBCursorPreparer {
-
DBCursor prepare(DBCursor cursor);
-
}
UpdateCallback
-
import com.mongodb.DBObject;
-
/**
-
* MongoDB更新操作接口定義
-
*
-
* @author: alex
-
* @time: 14-1-21 下午5:25
-
* @version: 1.0
-
*/
-
interface UpdateCallback {
-
DBObject doCallback(DBObject valueDBObject);
-
}