最近做了个用MongoDB存储的项目,由于MongoDB对开发人员不开放,由DBA统一管理。因此,自己抽时间写了个MongoDB的查询机。便于查询项目中的数据,方便维护和分析问题。总结了一些java操作MongoDB的方法。本文没有使用spring-data来操作MongoDB。而是直接使用MongoDB for java的原始API。现在介绍一下工具类。
1.准备工作
MongoDB的安装,不多做介绍。
MongoDB的java驱动
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.11.3</version> </dependency>
测试的集合:
2.封装实体介绍
- MongoDBCursor——MongoDB条件查询封装实体(支持limit,skip,sort)
- MongoDBEntity——MongoDB集合封装实体
- MongoDBUpdate——MongoDB更新封装实体
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<String, Object> sort = new LinkedHashMap<String, Object>(); /** * 自定义查询字段属性 */ private Map<String, Object> customFieldMap = new HashMap<String, Object>(); 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<String, Object> getSort() { return sort; } public void setSort(Map<String, Object> 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<String, Object> getCustomFieldMap() { return customFieldMap; } public void setCustomFieldMap(Map<String, Object> 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<String,Object> 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<String, Object> getFieldMap() { return fieldMap; } public void setFieldMap(Map<String, Object> 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<String, Object> whereMap; /** * value查询Map */ private Map<String, Object> valueMap; public Map<String, Object> getWhereMap() { return whereMap; } public void setWhereMap(Map<String, Object> whereMap) { this.whereMap = whereMap; } public Map<String, Object> getValueMap() { return valueMap; } public void setValueMap(Map<String, Object> valueMap) { this.valueMap = valueMap; } }
3.MongoDB操作类
- MongoDBCommonUtil——MongoDB集合操作工作类
- MongoDBUtil——MongoDBCRUD操作封装工具类
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<String, Object> map = new HashMap<String, Object>(); 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<DBObject> findAll(MongoDBCursor mongoDBCursor) { mongoDBCursor.setFieldMap(new HashMap<String, Object>()); return find(mongoDBCursor); } /** * 按条件查询 * 支持skip,limit,sort * * @param mongoDBCursor 查询实体 */ public static List<DBObject> 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<String, Object> 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<DBObject> 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<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); //查询 List<DBObject> result = MongoDBUtil.find(mongoDBCursor); for(DBObject dbObject : result){ for(String key : dbObject.keySet()){ System.out.println("key=" + key + "; value=" + dbObject.get(key)); } } } catch (Exception e) { e.printStackTrace(); } } }
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<String, Object> map = new HashMap<String, Object>(); 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<DBObject> findAll(MongoDBCursor mongoDBCursor) { mongoDBCursor.setFieldMap(new HashMap<String, Object>()); return find(mongoDBCursor); } /** * 按条件查询 * 支持skip,limit,sort * * @param mongoDBCursor 查询实体 */ public static List<DBObject> 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<String, Object> 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<DBObject> 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<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); }