logo 范 · 拾光录
网址收集 关于作者 Github Gitee
杂文随笔4
Hexo博客:基础使用Hexo博客:Next主题Hexo博客:Next进阶使用Hexo博客:Next高级配置
前端编程3
前端基础知识16
UniApp15
Vue框架19
Java编程7
Java基础知识14
SpringBoot31
SpringMVC14
MyBatis8
SpringCloud15
RocketMQ1
MyCat1
数据库4
MySQL6
Redis7
MongoDB10
H21
Java技术栈4
知识整理分布式锁的实现方案MySQL事务MySQL的锁
软件工具箱14
IDEAGitMavenGradleNginx安装Nginx配置JMeter压测OllamaPicGoRustFSVSCodeDockerObsidianObs录制
Linux知识11
树莓派安装及使用ArchLinux:常用软件ArchLinux:基础系统安装ArchLinux:深度优化ArchLInux:图形化界面安装ArchLinux:Nirifrp内网穿透Jar启动脚本Linux常用命令VirtualBox安装CentOSVirtualBox安装Ubuntu
Python编程6
Python基础知识Python语法yolo目标检测OpenCV的使用及树莓派平台condauv
创意设计2
Blender:入门知识UI设计基础知识
AI相关9
Claude CodeHermes AgentOpenAI基本使用OpenAI工具调用OpenAI记忆管理OpenAI推理执行OpenAI开发框架Langchainllama.cpp

按日期分组


Aggregation aggregation2 = Aggregation.newAggregation(
        Aggregation.match(Criteria.where("type").is(VisitRecordByDay.TYPE_SHOW_WEI_BASE_VISIT)),
        Aggregation.group().sum("time").as("count")
);

/**
 * 获取指定时间段的数据,按日期统计数量
 *
 * @param field          按数据库中哪个字段统计
 * @param startTimestamp 开始时间戳
 * @param endTimestamp   结束时间戳
 * @param format         格式化时间,%Y-%m-%d 按天,%Y-%m 按月
 * @return List<CountVo>
 */
public List<CountVo> getListDate(String field, Date startTimestamp, Date endTimestamp, String format, String collectionName, Criteria criteria) {
    Aggregation aggregation = Aggregation.newAggregation(
            Aggregation.match(Criteria.where(field).gte(startTimestamp).lt(endTimestamp).andOperator(criteria)),
            Aggregation.project(field).and(DateOperators.DateToString.dateOf(field).toString(format)).as("date"),
            Aggregation.group("date").count().as("count"),
            Aggregation.project("date", "count").and("date").previousOperation(),
            Aggregation.sort(Sort.Direction.ASC, "date")
    );
    return mongoTemplate.aggregate(aggregation, collectionName, CountVo.class).getMappedResults();
}

/**
 * 获取指定时间段的数据,按某字段数量和统计
 */
public List<CountVo> getListDateForSum(String field, Date startTimestamp, Date endTimestamp, String format, String collectionName, Criteria criteria, String sumFieldName) {
    Aggregation aggregation = Aggregation.newAggregation(
            Aggregation.match(Criteria.where(field).gte(startTimestamp).lt(endTimestamp).andOperator(criteria)),
            Aggregation.project(field, sumFieldName).and(DateOperators.DateToString.dateOf(field).toString(format)).as("date"),
            Aggregation.group("date").count().as("count2").sum(sumFieldName).as("count"),
            Aggregation.project("date", "count").and("date").previousOperation(),
            Aggregation.sort(Sort.Direction.ASC, "date")
    );
    return mongoTemplate.aggregate(aggregation, collectionName, CountVo.class).getMappedResults();
}

时区差8小时

根据时间统计会出现

Aggregation.project().andExpression("add(createTime," + 8 * 60 * 60 * 1000 + ")").as("newDate")

统计表中所有数据某字段相加

Aggregation.group().sum("time").as("count")

日期补全

使用的是hutool工具包做底层支持

/**
 * 补全一个月的所有天
 *
 * @param dateList       原数据
 * @param startTimestamp 开始时间戳
 * @param endTimestamp   结束时间戳
 * @return List<CountVo>
 */
private List<CountVo> supplementMonth(List<CountVo> dateList, long startTimestamp, long endTimestamp) {
    // 今日之后不显示
    if (endTimestamp > new Date().getTime()) {
        endTimestamp = new Date().getTime();
    }
    long stride = 24 * 60 * 60 * 1000; // 步长
    ArrayList<CountVo> listDate = new ArrayList<>();
    for (long newTime = startTimestamp; newTime <= endTimestamp; newTime += stride) {
        String formatDate = DateUtil.format(new Date(newTime), "yyyy-MM-dd");
        boolean isFind = false;
        for (CountVo countVo : dateList) {
            if (formatDate.equals(countVo.getDate())) {
                listDate.add(countVo);
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            listDate.add(new CountVo(formatDate, 0L));
        }
    }
    return listDate;
}


/**
 * 补全一年的所有月份
 *
 * @param dateList  原数据
 * @param startDate 一年开始时间
 * @return List<CountVo>
 */
private List<CountVo> supplementYear(List<CountVo> dateList, Date startDate) {
    ArrayList<CountVo> listDate = new ArrayList<>();
    for (int i = 0; i < 12; i++) {
        Date monthDate = DateUtil.offsetMonth(startDate, i);
        // 本月之后不显示
        if (monthDate.getTime() > new Date().getTime()) {
            break;
        }
        String formatDate = DateUtil.format(monthDate, "yyyy-MM");
        boolean isFind = false;
        for (CountVo countVo : dateList) {
            if (formatDate.equals(countVo.getDate())) {
                listDate.add(countVo);
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            listDate.add(new CountVo(formatDate, 0L));
        }
    }
    return listDate;
}

日期补全CountVo

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CountVo {

    private String date;
    private long count;
    
}
按日期分组
时区差8小时
统计表中所有数据某字段相加
日期补全
日期补全CountVo