最近做了个用MongoDB存储的项目,由于MongoDB对开发人员不开放,由DBA统一管理。因此,自己抽时间写了个MongoDB的查询机。便于查询项目中的数据,方便维护和分析问题。总结了一些java操作MongoDB的方法。本文没有使用spring-data来操作MongoDB。而是直接使用MongoDB for java的原始API。现在介绍一下工具类。
MongoDB的安装,不多做介绍。
MongoDB的java驱动
org.mongodb
mongo-java-driver
2.11.3
测试的集合:
MongoDBCursor
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* MongoDB条件查询封装实体(支持limit,skip,sort)
*
* @author: alex
* @time: 14-1-21 下午5:00
* @version: 1.0
*/
public class MongoDBCursor extends MongoDBEntity {
/**
* skip属性
*/
private int skip;
/**
* limit属性
*/
private int limit = 100;
/**
* 排序属性
*/
private Map sort = new LinkedHashMap();
/**
* 自定义查询字段属性
*/
private Map customFieldMap = new HashMap();
public int getSkip() {
return skip;
}
public void setSkip(int skip) {
this.skip = skip;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public Map getSort() {
return sort;
}
public void setSort(Map sort) {
this.sort = sort;
}
public DBObject getSortObject() {
if (this.sort == null) {
return null;
}
DBObject dbo = new BasicDBObject();
for (String k : sort.keySet()) {
dbo.put(k, Integer.valueOf(sort.get(k).toString()));
}
return dbo;
}
public Map getCustomFieldMap() {
return customFieldMap;
}
public void setCustomFieldMap(Map customFieldMap) {
this.customFieldMap = customFieldMap;
}
}
MongoDBEntity
import com.mongodb.DB;
import java.util.Map;
/**
* MongoDB集合封装实体
*
* @author: alex
* @time: 14-1-20 下午2:57
* @version: 1.0
*/
public class MongoDBEntity {
/**
* mongo数据库
*/
private DB db;
/**
* 集合名字
*/
private String collectionName;
/**
* 字段封装Map
*/
private Map fieldMap;
public DB getDb() {
return db;
}
public void setDb(DB db) {
this.db = db;
}
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
public Map getFieldMap() {
return fieldMap;
}
public void setFieldMap(Map fieldMap) {
this.fieldMap = fieldMap;
}
}
MongoDBUpdate
import java.util.Map;
/**
* MongoDB更新封装实体
*
* @author: alex
* @time: 14-1-21 下午5:00
* @version: 1.0
*/
public class MongoDBUpdate extends MongoDBEntity {
/**
* where查询Map
*/
private Map whereMap;
/**
* value查询Map
*/
private Map valueMap;
public Map getWhereMap() {
return whereMap;
}
public void setWhereMap(Map whereMap) {
this.whereMap = whereMap;
}
public Map getValueMap() {
return valueMap;
}
public void setValueMap(Map valueMap) {
this.valueMap = valueMap;
}
}
MongoDBCommonUtil
import com.mongodb.*;
import org.bson.types.BasicBSONList;
import org.bson.types.ObjectId;
import java.util.*;
/**
* MongoDBCRUD操作封装工具类
*
* @author: alex
* @time: 14-1-21 下午3:16
* @version: 1.0
*/
public class MongoDBUtil {
/**
* 按主键查询单个实体
*
* @param id 主键
* @param mongoDBCursor 查询实体
* @return DBObject
*/
public static DBObject findById(MongoDBCursor mongoDBCursor, String id) {
Map map = new HashMap();
map.put("_id", new ObjectId(id));
mongoDBCursor.setFieldMap(map);
return findOne(mongoDBCursor);
}
/**
* 按条件查询单个
*
* @param mongoDBCursor 查询实体
* @return DBObject
*/
public static DBObject findOne(MongoDBCursor mongoDBCursor) {
DBObject dbObject = getMapped(mongoDBCursor.getFieldMap());
return MongoDBCommonUtil.getCollection(mongoDBCursor).findOne(dbObject);
}
/**
* 查询全部
*
* @param mongoDBCursor 查询实体
*/
public static List findAll(MongoDBCursor mongoDBCursor) {
mongoDBCursor.setFieldMap(new HashMap());
return find(mongoDBCursor);
}
/**
* 按条件查询
* 支持skip,limit,sort
*
* @param mongoDBCursor 查询实体
*/
public static List find(MongoDBCursor mongoDBCursor) {
DBObject dbObject = getMapped(mongoDBCursor.getFieldMap());
BasicDBObjectBuilder customField = null;
//自定义查询字段
if(mongoDBCursor.getCustomFieldMap() != null && mongoDBCursor.getCustomFieldMap().size() > 0) {
customField = new BasicDBObjectBuilder();
for(Map.Entry field : mongoDBCursor.getCustomFieldMap().entrySet()) {
customField.add(field.getKey(),field.getValue());
}
}
return find(mongoDBCursor, dbObject, customField);
}
/**
* 查询(私有方法,检查是否含有skip,limit,sort)
*
* @param dbObject 查询条件
* @param mongoDBCursor 查询实体
*/
private static List find(final MongoDBCursor mongoDBCursor, DBObject dbObject,BasicDBObjectBuilder customField) {
MongoDBCursorPreparer cursorPreparer = mongoDBCursor == null ? null : new MongoDBCursorPreparer() {
public DBCursor prepare(DBCursor dbCursor) {
if (mongoDBCursor == null) {
return dbCursor;
}
if (mongoDBCursor.getLimit() <= 0 && mongoDBCursor.getSkip() <= 0 && mongoDBCursor.getSortObject() == null) {
return dbCursor;
}
DBCursor cursorToUse = dbCursor;
if (mongoDBCursor.getSkip() > 0) {
cursorToUse = cursorToUse.skip(mongoDBCursor.getSkip());
}
if (mongoDBCursor.getLimit() > 0) {
cursorToUse = cursorToUse.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 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 map) {
DBObject dbObject = new BasicDBObject();
Iterator> iterable = map.entrySet().iterator();
while (iterable.hasNext()) {
Map.Entry entry = iterable.next();
Object value = entry.getValue();
String key = entry.getKey();
if (key.startsWith("$") && value instanceof Map) {
BasicBSONList basicBSONList = new BasicBSONList();
Map conditionsMap = ((Map) value);
// Set 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
MongoDBUtil
import com.mongodb.*;
import org.bson.types.BasicBSONList;
import org.bson.types.ObjectId;
import java.util.*;
/**
* MongoDBCRUD操作封装工具类
*
* @author: alex
* @time: 14-1-21 下午3:16
* @version: 1.0
*/
public class MongoDBUtil {
/**
* 按主键查询单个实体
*
* @param id 主键
* @param mongoDBCursor 查询实体
* @return DBObject
*/
public static DBObject findById(MongoDBCursor mongoDBCursor, String id) {
Map map = new HashMap();
map.put("_id", new ObjectId(id));
mongoDBCursor.setFieldMap(map);
return findOne(mongoDBCursor);
}
/**
* 按条件查询单个
*
* @param mongoDBCursor 查询实体
* @return DBObject
*/
public static DBObject findOne(MongoDBCursor mongoDBCursor) {
DBObject dbObject = getMapped(mongoDBCursor.getFieldMap());
return MongoDBCommonUtil.getCollection(mongoDBCursor).findOne(dbObject);
}
/**
* 查询全部
*
* @param mongoDBCursor 查询实体
*/
public static List findAll(MongoDBCursor mongoDBCursor) {
mongoDBCursor.setFieldMap(new HashMap());
return find(mongoDBCursor);
}
/**
* 按条件查询
* 支持skip,limit,sort
*
* @param mongoDBCursor 查询实体
*/
public static List find(MongoDBCursor mongoDBCursor) {
DBObject dbObject = getMapped(mongoDBCursor.getFieldMap());
BasicDBObjectBuilder customField = null;
//自定义查询字段
if(mongoDBCursor.getCustomFieldMap() != null && mongoDBCursor.getCustomFieldMap().size() > 0) {
customField = new BasicDBObjectBuilder();
for(Map.Entry field : mongoDBCursor.getCustomFieldMap().entrySet()) {
customField.add(field.getKey(),field.getValue());
}
}
return find(mongoDBCursor, dbObject, customField);
}
/**
* 查询(私有方法,检查是否含有skip,limit,sort)
*
* @param dbObject 查询条件
* @param mongoDBCursor 查询实体
*/
private static List find(final MongoDBCursor mongoDBCursor, DBObject dbObject,BasicDBObjectBuilder customField) {
MongoDBCursorPreparer cursorPreparer = mongoDBCursor == null ? null : new MongoDBCursorPreparer() {
public DBCursor prepare(DBCursor dbCursor) {
if (mongoDBCursor == null) {
return dbCursor;
}
if (mongoDBCursor.getLimit() <= 0 && mongoDBCursor.getSkip() <= 0 && mongoDBCursor.getSortObject() == null) {
return dbCursor;
}
DBCursor cursorToUse = dbCursor;
if (mongoDBCursor.getSkip() > 0) {
cursorToUse = cursorToUse.skip(mongoDBCursor.getSkip());
}
if (mongoDBCursor.getLimit() > 0) {
cursorToUse = cursorToUse.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 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 map) {
DBObject dbObject = new BasicDBObject();
Iterator> iterable = map.entrySet().iterator();
while (iterable.hasNext()) {
Map.Entry entry = iterable.next();
Object value = entry.getValue();
String key = entry.getKey();
if (key.startsWith("$") && value instanceof Map) {
BasicBSONList basicBSONList = new BasicBSONList();
Map conditionsMap = ((Map) value);
// Set 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
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);
}