这两天要做mongodb日志的模块,下面记录一下。

一、

首先要导入一批数据,使用springboot来完成。

配置mongodb的复制集:在application.yml文件中配置uri来完成

格式:mongodb://用户名:密码@ip:端口[,ip:端口] [,ip:端口]/数据库名

下面注入mongoTemplate:

二、

Mongodb在java中分组使用应该有两三种方式:可以百度一下

下面我贴上我们项目组使用的代码:

@Service("logMongoService")

public class LogMongoServiceImpl implements LogMongoService {

/**

* 按月统计登录次数使用的初始化的js对象

*/

private static final String initMonthGroupByJs = "{total:0," +

"JanCount:0," +

"FebCount:0," +

"MarCount:0," +

"AprCount:0," +

"MayCount:0," +

"JuneCount:0," +

"JulyCount:0," +

"AugCount:0," +

"SepCount:0," +

"OctCount:0," +

"NovCount:0," +

"DecCount:0}";

/**

* 按月统计登录次数使用的js,mongodb在执行时会自动的执行此脚本

*/

private static final String monthGroupByJs =

"function(doc, prev){" +

"prev.total += 1;" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 0) {" +

"prev.JanCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 1) {" +

"prev.FebCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 2) {" +

"prev.MarCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 3) {" +

"prev.AprCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 4) {" +

"prev.MayCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 5) {" +

"prev.JuneCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 6) {" +

"prev.JulyCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 7) {" +

"prev.AugCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 8) {" +

"prev.SepCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 9) {" +

"prev.OctCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 10) {" +

"prev.NovCount += 1;" +

"}" +

"if (doc.startTime.getFullYear() == searchYear && doc.startTime.getMonth() == 11) {" +

"prev.DecCount += 1;" +

"}" +

"}";

@Override

public void findMonthLogin(MongoPage page, SystemLogQuery systemLogQuery) throws Exception{

//添加年份的限制  也就是查询条件

Criteria[] logType ={Criteria.where("logType").is("2")};

Calendar startCalendar = Calendar.getInstance();

startCalendar.set(year, 0, 1, 0,0, 0);

Calendar endCalendar = Calendar.getInstance();

endCalendar.set(year + 1, 0, 1, 0,0, 0);

Criteria[] yearCriteria ={Criteria.where("startTime").gte(startCalendar.getTime()).lt(endCalendar.getTime())};

logType = (Criteria[]) ArrayUtils.addAll(logType, yearCriteria);

//组织分组时使用的js

String initGroupByJs = initMonthGroupByJs;

String groupByJs = monthGroupByJs.replace("searchYear", year + "");

//此处开始分组查询

//以name字段进行分组

GroupBy groupBy = new GroupBy("username","name","departmentName").

initialDocument(initGroupByJs).

reduceFunction(groupByJs);

//执行分组查询

GroupByResults groupByResults = logMongoDao.group(new Criteria().andOperator(logType), groupBy);

//取出分组后的数据 这里必须是retval

BasicDBList list = (BasicDBList) groupByResults.getRawResults().get("retval");

page.setTotal(list.size());

//用来存放查询到的list集合

List<SystemLogVO> logVOList = new ArrayList<SystemLogVO>();

for (int i = (page.getPageNumber()-1) * page.getPageSize();

i < page.getPageNumber() * page.getPageSize() && i < list.size(); i++) {

SystemLogVO systemlog = new SystemLogVO();

//在这个obj中就包含了所有的分组后,我们自定义的哪些属性的值

BasicDBObject obj = (BasicDBObject)list.get(i);

systemlog.setName(obj.get("name") == null? "" : obj.get("name").toString());

systemlog.setDepartmentName(obj.get("departmentName") == null?"":obj.get("departmentName").toString());

systemlog.setUsername(obj.get("username") == null?"":obj.get("username").toString());

//获取其中的月份的值

obj.get("total");//总的值

obj.get("JanCount");//根据条件过滤后,统计的1月份的值

obj.get("DecCount");

}

}

}

需要说明的一点是,groupByJs这个是一个回调函数,

当mongodb在过滤数据时,会调用这个js方法,doc变量是将正在处理的一条数据传输进来,prev相当于上面定义的initMonthGroupByJs中的js对象,

最后在initMonthGroupByJs中定义的对象会返回给我们,我们可以从prev中取到我们所有需要的数据。

Addition:

https://blog.csdn.net/liming_0820/article/details/78657146

http://www.cnblogs.com/anhaogoon/p/9354248.html

mongodb分组函数的使用(spring-data-mongodb)的更多相关文章

  1. Spring Data MongoDB 一:入门篇(环境搭建、简单的CRUD操作)

    一.简介 Spring Data  MongoDB 项目提供与MongoDB文档数据库的集成.Spring Data MongoDB POJO的关键功能区域为中心的模型与MongoDB的DBColle ...

  2. spring data mongodb 配置遇到的几个问题

    一. mongodb 2.2版本以上的配置 spring.data.mongodb.uri = mongodb://newlook:newlook@192.168.0.109:27017/admin ...

  3. spring data mongodb中,如果对象中的属性不想加入到数据库字段中

    spring data mongodb中,如果对象中的属性不想加入到数据库字段中,可加@Transient注解,声明为透明属性 spring data mongodb 官网帮助文档 http://ww ...

  4. Spring Data MongoDB example with Spring MVC 3.2

    Spring Data MongoDB example with Spring MVC 3.2 Here is another example web application built with S ...

  5. 使用Spring访问Mongodb的方法大全——Spring Data MongoDB查询指南

    1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...

  6. Spring data mongodb 聚合,投射,内嵌数组文档分页.

    尽量别直接用 DBObject  ,Spring data mongodb 的api 本来就没什么多大用处,如果还直接用 DBObject 那么还需要自己去解析结果,说动做个对象映射,累不累 Spri ...

  7. JAVA 处理 Spring data mongodb 时区问题

    Spring data mongodb 查询出结果的时候会自动 + 8小时,所以我们看起来结果是对的 但是我们查询的时候,并不会自动 + 8小时,需要自己处理 解决方法 1   @JsonFormat ...

  8. Spring data mongodb ObjectId ,根据id日期条件查询,省略@CreatedDate注解

    先看看ObjectId 的json 结构,非常丰富,这里有唯一机器码,日期,时间戳等等,所以强烈建议ID 使用 ObjectId 类型,并且自带索引 Spring data mongodb 注解 @C ...

  9. Spring data mongodb @CreatedBy@LastModifiedBy@CreatedBy@LastModifiedBy SpringSecurityAuditorAware,只记录用户名

    要在Spring data mongodb 中使用@CreatedBy@LastModifiedBy@CreatedBy@LastModifiedBy  这四个注解 必须实现 SpringSecuri ...

  10. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)

    一.简单介绍 Spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...

随机推荐

  1. php备注

    一.关于OOP 1.PHP目前不支持方法重载

  2. Java线程唤醒与阻塞

    阻塞指的是暂停一个线程的执行以等待某个条件发生(如某资源就绪),学过操作系统的同学对它一 定已经很熟悉了.Java 提供了大量方法来支持阻塞,下面让我们逐一分析. 转载于:http://blog.cs ...

  3. Python爬虫教程-02-使用urlopen

    Spider-02-使用urlopen 做一个最简单的python爬虫,使用爬虫爬取:智联招聘某招聘信息的DOM urllib 包含模块 - urllib.request:打开和读取urls - ur ...

  4. Selenium IDE录制脚本

    一.当Selenium IDE中Format没有转换格式时,操作:Options—Options...—勾选Enable experimental features

  5. 一分钟在云端快速创建MySQL数据库实例

    本教程将帮助您了解如何使用Azure管理门户迅速创建,连接,配置MySQL 数据库 on Azure.完成本教程后,您将在Azure上拥有一个示例MySQL数据库服务器,并了解如何使用管理门户执行基本 ...

  6. [NCH 1, 3]

    Preview: 1. Implement strStr() O(m*n): class Solution { public: int strStr(string haystack,string ne ...

  7. 安装Xcode主题

    安装Xcode主题 下载地址 https://github.com/YouXianMing/Xcode-Themes 安装教程 1. 安装文件夹中的字体 2. 如下图,执行 ./cp_themes.s ...

  8. 测试你的 In-app Billing 程序

    测试你的 In-app Billing 程序 为了保证 In-app Billing 可以在你程序中正常使用,你应该在把应用程序发布到Google Play之前进行测试.早期的测试有助于确保用户对于你 ...

  9. netstat 常用方法

    netstat简介 netstat是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表,实际的网络连接以及每一个网络接口设备的状态信息,netstat用于显示与IP,TCP,UDP和ICMP协 ...

  10. ZT自老罗的博客 Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理分析

    Android系统的智能指针(轻量级指针.强指针和弱指针)的实现原理分析 分类: Android 2011-09-23 00:59 31568人阅读 评论(42) 收藏 举报 androidclass ...