• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

common-mongodb: mongodb操作工具类封装库

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

common-mongodb

开源软件地址:

https://gitee.com/cnsugar/common-mongodb

开源软件介绍:

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。

测试代码

    @org.junit.Test    public void testSave() {        //保存实体对象        TestEntity entity = new TestEntity();        entity.setName("sugar");        entity.setStatus(2);        entity.setTestScore(87f);        MongoUtils.save(entity);        //保存map对象,需要指定集合名称        Map<String, Object> 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<TestEntity> ll = MongoUtils.findAll(TestEntity.class);        System.out.println(ll);        //查询Document对象列表,需要指定集合名        List<Document> list = MongoUtils.findAll("test");        System.out.println(list);        //用Filters生成条件查询,查询名字以2结尾的数据        List<TestEntity> 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<ObjectId> del = new ArrayList<ObjectId>();        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<Document> 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<Document> list2 = MongoUtils.mapReduce("test", mapFunction.toString(), reduceFunction.toString());        System.out.println(list2);    }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap