首页 > 其他分享 >spring 使用聚合Aggregation substr mongo 查询

spring 使用聚合Aggregation substr mongo 查询

时间:2023-04-07 18:13:38浏览次数:50  
标签:core mongo spring Aggregation springframework mongodb dataJson org import

import com.xxx.repository.kpi.entity.ChannelCheckBase;
import com.xxx.repository.kpi.entity.DataJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.ScriptOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.script.ExecutableMongoScript;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @autor wangy
 * @date 2021/3/16 13:33
 **/
@Service
public class ChannelResCountStatisticsService {

    private static final Logger logger = LoggerFactory.getLogger(ChannelResCountStatisticsService.class);
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据mongo中的目录ID、发布时间,统计每天、每个栏目下的资源量
     * @return
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public DataJson queryChannelResCountStatisticsByDay(){
        System.out.println("new date()===>" + new Date());
        DataJson dataJson = new DataJson();
        try {
            ScriptOperations scriptOps = mongoTemplate.scriptOps();
            String sqlStr = "db.getCollection(\"REPO_MANAGE_INFO\").aggregate(\n" +
                    "    {\n" +
                    "        $match: \n" +
                    "        {\n" +
                    "            'st': '3',\n" +
                    "            \"$and\": [{\n" +
                    "                \"metd.Pubtime\": {\n" +
                    "                    \"$gte\": \"2021-02-19 00:00:00\"\n" +
                    "                }\n" +
                    "            }, {\n" +
                    "                \"metd.Pubtime\": {\n" +
                    "                    \"$lte\": \"2021-02-20 23:59:59\"\n" +
                    "                }\n" +
                    "            }]\n" +
                    "        }\n" +
                    "    }\n" +
                    "    ,\n" +
                    "    {\n" +
                    "        $group: \n" +
                    "        {\n" +
                    "            \"_id\": {\n" +
                    "                dir: \"$dir\",\n" +
                    "                Year: {\n" +
                    "                    $substr: [\"$metd.Pubtime\", 0, 4]\n" +
                    "                },\n" +
                    "                Month: {\n" +
                    "                    $substr: [\"$metd.Pubtime\", 5, 2]\n" +
                    "                },\n" +
                    "                Day: {\n" +
                    "                    $substr: [\"$metd.Pubtime\", 8, 2]\n" +
                    "                }\n" +
                    "            },\n" +
                    "            \"orderNum\": {\n" +
                    "                \"$sum\": 1\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    ");";
            ExecutableMongoScript echoScript = new ExecutableMongoScript(sqlStr);
            // 执行mongo语句,并得到查询结果
            Object directly_execute_script = scriptOps.execute(echoScript, "directly execute script");

            if (1==1) {
                dataJson.setMsg("数据保存成功!");
                dataJson.setStatus("0");
            } else {
                dataJson.setMsg("数据保存失败!");
                dataJson.setStatus("1");
            }
            return dataJson;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            dataJson.setMsg(e.getMessage());
            dataJson.setStatus("1");
            return dataJson;
        }
    }
} 
import com.xxx.repository.kpi.entity.ChannelCheckBase;
import com.xxx.repository.kpi.entity.DataJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.ScriptOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.script.ExecutableMongoScript;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @autor wangy
 * @date 2021/3/16 13:33
 **/
@Service
public class ChannelResCountStatisticsService {

    private static final Logger logger = LoggerFactory.getLogger(ChannelResCountStatisticsService.class);
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据mongo中的目录ID、发布时间,统计每天、每个栏目下的资源量
     * @return
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public DataJson queryChannelResCountStatisticsByDay(){
        System.out.println("new date()===>" + new Date());
        DataJson dataJson = new DataJson();
        try {
            Criteria criteria = Criteria.where("st").is("3");
            criteria.andOperator(Criteria.where("metd.Pubtime").lte("2021-02-20 23:59:59"),
                    Criteria.where("metd.Pubtime").gte("2021-02-19 00:00:00"));
            Aggregation customerAgg = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.project("dir","year","month","day")
                            .andExpression("{ dir: '$dir'," +
                            "                year: { " +
                            "                    $substr: {'$metd.Pubtime', 0, 4}" +
                            "                }," +
                            "                Month: {" +
                            "                    $substr: {'$metd.Pubtime', 5, 2}" +
                            "                }," +
                            "                Day: {" +
                            "                    $substr: {'$metd.Pubtime', 8, 2}" +
                            "                } " +
                            "}").as("_id"),
                    Aggregation.group("_id").count().as("updateNum")
            );
            // 进行查询,并得到查询结果
            AggregationResults<ChannelCheckBase> aggregate = mongoTemplate.aggregate(customerAgg, "REPO_MANAGE_INFO", ChannelCheckBase.class);
            aggregate.getMappedResults();
            if (1==1) {
                dataJson.setMsg("数据保存成功!");
                dataJson.setStatus("0");
            } else {
                dataJson.setMsg("数据保存失败!");
                dataJson.setStatus("1");
            }
            return dataJson;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            dataJson.setMsg(e.getMessage());
            dataJson.setStatus("1");
            return dataJson;
        }
    }
} 

 

标签:core,mongo,spring,Aggregation,springframework,mongodb,dataJson,org,import
From: https://www.cnblogs.com/daixs/p/17297014.html

相关文章

  • centos7 mongodb4.4分片集群部署
    #创建mongo相应的目录mkdir-pv/data/app/mongodb/confmkdir-pv/data/app/mongodb/{configset,shared1,shared2,shared3}/logmkdir-pv/data/mongodb/{configset,shared1,shared2,shared3}/data #创建于用户,给权限useradd-s/sbin/nologin-Mmongodchown-Rmong......
  • 使用Docker发布普通web项目(非springboot项目)
    公司的项目比较旧,还是普通的web格式,只能打个war包放在tomcat中运行,现在要上docker发布,在此做下记录DockerDesktop开发机是win10,想要打包镜像需要安装DockerDesktop,安装前需要在bios里面开启虚拟化支持。然后按照官方安装教程一步一步安装。Dockerfile然后在项目根目录编写doc......
  • Springfox与SpringDoc——swagger如何选择(SpringDoc入门)
     本文分享自天翼云开发者社区@《Springfox与SpringDoc——swagger如何选择(SpringDoc入门)》,作者:才开始学技术的小白  0.引言之前写过一篇关于swagger(实际上是springfox)的使用指南(https://www.ctyun.cn/developer/article/371704742199365),涵盖了本人在开发与学习的时候碰......
  • 基于SpringBoot实现单元测试的多种情境/方法(二)
    本文分享自天翼云开发者社区@《基于SpringBoot实现单元测试的多种情境/方法(二)》,  作者:才开始学技术的小白 1Mock基础回顾在上一篇分享中我们详细介绍了简单的、用mock来模拟接口测试环境的方法,具体的使用样例我们再回顾一下:1.首先是最简单的不需要传参的示例,需要注意的是,......
  • SpringSecurity实现权限系统设计
    RBAC权限分析RBAC全称为基于角色的权限控制,本段将会从什么是RBAC,模型分类,什么是权限,用户组的使用,实例分析等几个方面阐述RBAC思维导图绘制思维导图如下什么是RBACRBAC全称为用户角色权限控制,通过角色关联用户,角色关联权限,这种方式,间阶的赋予用户的权限,如下图所示对于通常的系统而......
  • 【Spring原理分析-Scope+Spring Boot自定义静态资源映射】
    Spring原理分析-Scope本文纲要:一、Scope1、Scope类型2、基础准备与演示二、Scope失效1、基础准备与演示2、失效原因及处理3、总结下面开始进入正文:一、Scope1、Scope类型singleton,prototype,request,session,application2、基础准备与演示①编写各种Scope的Beanrequest......
  • 如何在 Spring Boot 应用程序中使用 Actuator 监控和管理端点,提高应用程序的生产力?
    1概述1.1整合添加依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>启动应用,会观察到如下日志:13:15:17.642ifp[main]INFOo.s.b.a.e.web.EndpointLin......
  • 23Error-Resilient Masking Approaches for Privacy Preserving Data Aggregation
    ......
  • SpringBoot2核心技术篇(自动配置原理入门[二])
    自动配置原理入门3.1引导加载自动配置类@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFilters={@Filter(type=FilterType.CUSTOM,classes={TypeExcludeFilter.class}),@Filter(type=FilterType.CUSTOM,classes=......
  • 动力节点王鹤SpringBoot3笔记——第四章 访问数据库
    视频:动力节点SpringBoot3从入门到项目实战第四章访问数据库SpringBoot框架为SQL数据库提供了广泛的支持,既有用JdbcTemplate直接访问JDBC,同时支持“objectrelationalmapping”技术(如Hibernate,MyBatis)。SpringData独立的项目提供对多种关系型和非关系型数据库的访问支持。......