Java实现MongoDB聚合管道

 public  static AggregateIterable<Document> groupCountBySiteId(String devId, String dataType, String startDateTime, String endDateTime, String fileName, String collectionName) {
        //连接数据库  collectionName
        MongoDatabase md = MongoManager.getMongoInstance().getDatabase(collectionName);
        //创建sql
        List<Document> dbObjects = new ArrayList<>();
        //用来写查询的
        Document document=new Document();
        document.append("did",devId);
        document.append("datatype",dataType);
        document.append("time",new Document().append("$gte", startDateTime).append("$lte", endDateTime));
        //where
        Document match =new Document("$match",document);
        Document sub_project =new Document();
        //截取用来分组的时间 用来group
        sub_project.put("time",new Document("$substrBytes", Arrays.asList(new Object[]{"$time",0,13})));
        sub_project.put("value","$value");
        Document project = new Document("$project",sub_project);
        //group
        Document sub_group = new Document();
        sub_group.put("_id","$time");
        sub_group.put("value",new Document("$first","$value"));
        sub_group.put("time",new Document("$first","$time"));
        Document group = new Document("$group",sub_group);
        //order by
        Document sort = new Document("$sort",new Document("time", 1));
        dbObjects.add(match);
        dbObjects.add(project);
        dbObjects.add(group);
        dbObjects.add(sort);
        AggregateIterable<Document> x =md.getCollection(fileName).aggregate(dbObjects);
        MongoCursor<Document> it=x.iterator();
        while (it.hasNext()){
            Document document1=it.next();
            System.out.println(document1.toJson());
        }
        return x;
    }
///**
// * 业务查询 支持切换数据库 支持切换数据表名字
// *
// * @param devId
// * @param dataType
// * @param startDateTime
// * @param endDateTime
// * @param fileName
// * @param collectionName
// * @return
// */
//public static FindIterable<Document> queryHistory(String devId, String dataType, String startDateTime, String endDateTime, String fileName, String collectionName) {
//    MongoDatabase md = MongoManager.getMongoInstance().getDatabase(collectionName);
//    FindIterable<Document> iterable = md.getCollection(fileName).find(
//            new Document().append("did", devId).append("datatype", dataType)
//                    .append("time", new Document().append("$gte", startDateTime).append("$lte", endDateTime).append("time", new Document().append("$group",startDateTime)))
//    );
//    return iterable;
//}

在这里插入图片描述

在这里插入图片描述

使用springdata 提供的Aggregation.newAggregation() 写法

Aggregation aggregation = Aggregation.newAggregation(
Aggregation.project("_id","uri","scope","desc","pushState","collectState","collectInterval",
"lastCollectTime","refreshRequestHeaders","effectivePercent","hasDataVersion",
"childTaskAlivePeriod","parentId","createdTime","lastModifiedTime",
"lastCollectValue","lastPushValue","expireFlag","nextCollectTime")
.andExpression("createdTime + childTaskAlivePeriod * 1000").as("expireTime"),
Aggregation.match(new Criteria()
.orOperator(Criteria.where("expireFlag").is(true), 
new Criteria().andOperator(Criteria.where("collectState")
.is(1), Criteria.where("nextCollectTime").lt(now))
, Criteria.where("expireTime").lt(now))));
AggregationResults<RefreshTaskDO> refreshTask = mongodb
.aggregate(aggregation, "RefreshTask", RefreshTaskDO.class);
List<RefreshTaskDO> mappedResults = refreshTask.getMappedResults();
 /**
     * 曲线秒级  需要改成前端传 %Y-%m-%d %Y-%m-%d HH:MM:SS根据改变
     *
     * @param sn
     * @param pid
     * @param startDateTime
     * @param endDateTime
     * @return
     */
    public List<DlHistory> history(String sn, String pid, String startDateTime, String endDateTime, String type) {
        List<DlHistory> dlHistoryList = new ArrayList<>();
        if ("小时".equals(type)) {
            Query query = new Query(Criteria.where("sn").is(sn).and("pid")
                    .is(Integer.valueOf(pid)).and("time").gte(startDateTime).lte(endDateTime));
            Sort ageSort = Sort.by(Sort.Direction.ASC, "time");
            query.with(ageSort);
            dlHistoryList = mongoTemplate.find(query, DlHistory.class);
        } else if ("自定义".equals(type)) {
			  Criteria criteria = new Criteria();
            criteria.andOperator(Criteria.where("sn").is(sn).and("pid").is(Integer.valueOf(pid)).and("time").gte(startDateTime).lte(endDateTime));
            // 匹配查询
            MatchOperation matchOperation = Aggregation.match(criteria);// localPath
            // 返回参数
            ProjectionOperation return1 = Aggregation.project("val","pid","sn","time").andExpression("substr(time,0,13)").as("hours");
            // 按条件分组
            GroupOperation go2 = Aggregation.group("hours").first("sn").as("sn").first("pid").as("pid").first("val").as("val").first("hours").as("time");
            // 设置排序
            SortOperation sortOperation = Aggregation.sort(Sort.Direction.ASC, "_id");
            // 构建参数
            Aggregation aggregation = Aggregation.newAggregation(matchOperation, return1, go2, sortOperation);
            // 分组聚合查询
            AggregationResults<DlHistory> aggregate = mongoTemplate.aggregate(aggregation, DlHistory.class, DlHistory.class);
            // 获取结果
            dlHistoryList = aggregate.getMappedResults();
        }
        return dlHistoryList;
    }
public void queryAggregates() {
        List<Bson> bsons = new ArrayList<>();
        Bson id = Aggregates.match(Filters.eq("id", 5));
        Bson match = Aggregates.match(Filters.eq("users.learns.id", 3));
        Bson unwind = Aggregates.unwind("$users");
        Bson match2 = Aggregates.match(Filters.eq("users.learns.id", 3));
        Bson sort = Aggregates.sort(Sorts.descending("users.learns.time"));
        Bson limit = Aggregates.limit(5);
        Bson project = Aggregates.project(Projections.slice("users.learns", 2,3));
        bsons.add(id);
        bsons.add(match);
        bsons.add(unwind);
        bsons.add(match2);
        bsons.add(sort);
        bsons.add(limit);
        bsons.add(project);
        AggregateIterable<Document> aggregate = c.aggregate(bsons);
        for (Document document : aggregate) {
            Object users = document.get("users");
            System.out.println(users);
        }
    }

没有学会的话 可以去这篇文章去细学一下 理解意思后 就比较容易写了
https://zhuanlan.zhihu.com/p/90291743

将上面语句转到JAVA中的语句时候需要将"[…]“修改为”{…}"

知识点

  1. $match:条件匹配
  2. $group :分组
  3. $sort :排序
  4. $limit :限制结果返回数量

MongoDB 查询:组内分组排序

db.getCollection('order').aggregate([
{"$match":{"shop_name":"A"}},
{"$sort":{"create_time":1}},   ## 1表示升序,-1表示降序
{"$group":{"_id":"$userId",            ## 类似MySql的group by userId
	"create_time ":{"$first":"$create_time "},
	"price":{"$first":"$price"} }},
{"$limit":1}  ## 查询结果返回1条
])
 /**
     * 每个时间只能有一条数据
     * @param devId
     * @param strArr
     * @param startDateTime
     * @param endDateTime
     * @param fileName
     * @param collectionName
     * @return
     */
    public  static AggregateIterable<Document> queryManyHistory(String devId, String[] strArr, String startDateTime, String endDateTime, String fileName, String collectionName) {
        //连接数据库  collectionName
        MongoDatabase md = MongoManager.getMongoInstance().getDatabase(collectionName);

        //创建sql
        List<Document> dbObjects = new ArrayList<>();
        //用来写查询的
        Document document=new Document();
        document.append("did",devId);
        document.append("datatype",new Document("$in", Arrays.asList(strArr)));
        document.append("time",new Document().append("$gte", startDateTime).append("$lte", endDateTime));
        //where
        Document match =new Document("$match",document);

        Document c = new Document("$substrBytes", Arrays.asList(new Object[]{"$time",0,13}));
        Document sub_project =new Document();
        //截取用来分组的时间 用来group
        sub_project.put("time",c);
        sub_project.put("did","$did");
        sub_project.put("datatype","$datatype");
        //group
        Document sub_group = new Document();
        sub_group.put("_id",sub_project);
        sub_group.put("value",new Document("$first","$value"));
        sub_group.put("time",new Document("$first",c));
        sub_group.put("did",new Document("$first","$did"));
        sub_group.put("dataType",new Document("$first","$datatype"));
        Document group = new Document("$group",sub_group);

        //order by
        Document sort = new Document("$sort",new Document("time", 1));
        dbObjects.add(match);
        dbObjects.add(group);
        dbObjects.add(sort);
        AggregateIterable<Document> aggregateIterable =md.getCollection(fileName).aggregate(dbObjects);
        MongoCursor<Document> it=aggregateIterable.iterator();
        while (it.hasNext()){
            Document document1=it.next();
            System.out.println(document1.toJson());
        }
        return aggregateIterable;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想要一百块

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值