# common-mongodb **Repository Path**: cnsugar/common-mongodb ## Basic Information - **Project Name**: common-mongodb - **Description**: mongodb操作工具类封装库 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 11 - **Forks**: 4 - **Created**: 2019-01-07 - **Last Updated**: 2024-08-15 ## Categories & Tags **Categories**: database-dev **Tags**: None ## README # common-mongodb #### 介绍 mongodb操作工具类封装库,减少mongodb操作难度,支持直接save或update java对象,支持批量对象操作,支持分页查询,支持自定义复杂的函数。 经实际测试,性能比使用spring-data-mongodb更高。 #### 使用说明 1. 直接使用MongoUtils中的静态方法; 2. mongodb配置文件祥见test/resources目录下的文件; 3. java对象与mongodb中表的字段映射关系或顺序,可通过fastjson中的@JSONField注解进行配置; 4. 详细使用介绍请参阅https://www.cnblogs.com/cnsugar/p/10239201.html。 #### 测试代码 ```java @org.junit.Test public void testSave() { //保存实体对象 TestEntity entity = new TestEntity(); entity.setName("sugar"); entity.setStatus(2); entity.setTestScore(87f); MongoUtils.save(entity); //保存map对象,需要指定集合名称 Map map = new HashMap<>(); map.put("name", "zhangshan"); map.put("status", 2); map.put("test_score", 82.5f); map.put("onTime", new Date().getTime()); MongoUtils.save(map, "test"); //保存Document对象,与map类似,需要指定集合名称 Document doc = new Document(); doc.put("name", "lishi"); doc.put("status", 2); doc.put("test_score", 99f); doc.put("onTime", new Date().getTime()); MongoUtils.save(doc, "test"); } @org.junit.Test public void testUpdate() { //更新java对象,必须要指定id,如果字段值为null,不会更新旧的数据 TestEntity entity = new TestEntity(); entity.setId("5c343804fdfad4230852e1f5"); entity.setName("sugar2"); entity.setStatus(1); MongoUtils.update(entity); //自定义更新的集合名、条件、字段 String collectionName = "test"; Bson filter = Filters.eq("_id", new ObjectId("5c343804fdfad4230852e1f6")); Update update = new Update(); update.set("name", "zhangshan2"); update.inc("status", 1);//相当于status += 1 MongoUtils.update(collectionName, filter, update); } @org.junit.Test public void testQuery() { //查询出实体列表 List ll = MongoUtils.findAll(TestEntity.class); System.out.println(ll); //查询Document对象列表,需要指定集合名 List list = MongoUtils.findAll("test"); System.out.println(list); //用Filters生成条件查询,查询名字以2结尾的数据 List ll2 = MongoUtils.find(TestEntity.class, Filters.regex("name", ".*2")); System.out.println(ll2); //分页查询,查询分数大于90的数据,查询第1页,每页10条 Page page = new Page(10, 1); page.setClazz(TestEntity.class);//指定列表中的对象类型 page = MongoUtils.findPage(page, Filters.gt("test_score", 90)); System.out.println(page.getList()); } @org.junit.Test public void testDelete() { //根据ID删除 MongoUtils.deleteById("test", "587482defdfad41a9c94c9b6"); //删除一条数据 MongoUtils.deleteOne("test", Filters.eq("_id", new ObjectId("587482defdfad41a9c94c9b6"))); //批量删除 List del = new ArrayList(); del.add(new ObjectId("587482defdfad41a9c94c9b6")); del.add(new ObjectId("58748350fdfad41a1c5fba14")); del.add(new ObjectId("5874930ffdfad40df031215a")); MongoUtils.deleteAll("test", Filters.in("_id", del)); } @org.junit.Test public void testCount() { //统计test表数据总数 long count = MongoUtils.count("test"); //统计test表中status=2的数据总数 long count2 = MongoUtils.count("test", Filters.eq("status", 2)); //根据status进行分组统计 List list = MongoUtils.count("test", new String[]{"status"}); System.out.println(list); //自定义mapReduce函数进行数据分析,按天统计数据总数和status=1的总数 StringBuilder mapFunction = new StringBuilder("function(){emit("); mapFunction.append("new Date(this.onTime).toLocaleDateString()"); mapFunction.append(",{count:1, send:this.status==1?1:0}"); mapFunction.append(");}"); StringBuilder reduceFunction = new StringBuilder("function(key, values){"); reduceFunction.append("var _total = 0, _send = 0;"); reduceFunction.append("values.forEach(function(val){_total += val.count; _send += val.send;});"); reduceFunction.append("return {count:_total, send:_send};"); reduceFunction.append("}"); List list2 = MongoUtils.mapReduce("test", mapFunction.toString(), reduceFunction.toString()); System.out.println(list2); } ```