mongoDB 分组并对分组结果筛选类似于SQL中的(group by xxx having ) 附带Java代码
今天需要做一个筛选程序,因为数据放在mongodb中,没写过分组的查询语句,查了一些资料,终于写出来了,分享给各位小伙伴
需求是 查询 学员 在2019-07-29之后未同步的数据(同一个学员需要2条数据或以上才符合同步条件)
这是mongoDB原生语句
db.getCollection("ClassRecordOneDetail").aggregate([ //下面相当于sql 里面的 where synState = 0 and starttime >=to_date('2019-08-02','yyyy-MM-dd') {$match:{synState:0,"starttime":{$gte:new Date("2019-08-02")}}}, //下面相当于sql 里面的group by分组 分组字段为 stunum,starttime去掉了时分秒分组 {$group:{_id:{stunum : "$stunum" , starttime: {
month: { $month: "$starttime" },
day: { $dayOfMonth: "$starttime" },
year: { $year: "$starttime"}
}},counter:{$sum:1}}}, //下面相当于sql 里面的 having count(1) >1 {$match:{counter:{$gt:1}}}
]);
这是java代码
import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoCollection; import org.bson.Document; import com.mongodb.client.MongoCursor; /** *这里我就写出了mongoDB我使用的类,其他的导入类型我没写。 * **/ MongoCollection<Document> mc = MongoDBUtil.instance.getCollection("dzwl", "ClassRecordOneDetail"); Document sub_match = new Document();
sub_match.put("subState", 0);
sub_match.put("starttime", new Document("$gte", date)); Document sub_group = new Document();
sub_group.put("_id", new Document("stunum","$stunum").append(
"starttime", new Document("month",new Document("$month","$starttime")).append("day",new Document("$dayOfMonth","$starttime")).append("year",new Document("$year","$starttime"))
));
sub_group.put("counter", new Document("$sum", 1)); Document match = new Document("$match", sub_match);
Document group = new Document("$group", sub_group);
Document match2 = new Document("$match", new Document("counter",new Document("$gt", 1))); List<Document> documents = new ArrayList<>();
documents.add(match);
documents.add(group);
documents.add(match2); MongoCursor<Document> cursor = mc.aggregate(documents).iterator(); //下面直接遍历查询查来的数据就可以了 MongoCursor<Document> cursor = mc.aggregate(documents).iterator();
try {
while(cursor.hasNext()){ } } catch (Exception e) {
// TODO: handle exception
}
mongoDB 分组并对分组结果筛选类似于SQL中的(group by xxx having ) 附带Java代码的更多相关文章
- NSPredicate用法总结(Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取)
简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 定义(最常用到的方法): NSPredicate *ca = [NSPred ...
- C#EF中,使用类似于SQL中的% 模糊查询
最近在做项目的时候需要使用到模糊查询,但是后台使用EF写的 而不是ADO或者是Dapper,如果是这样的话,我们就可以使用Sql语句直接进行模糊查询 现在我们需要在LINQ中使用类似于模糊查询 在EF ...
- C# Lamda中类似于SQL 中的 In 功能
首先,在程序中接受一个数组 例如:int[] s=[1,2,3]; 在Lamda表达式中使用如下: db.userinfo.where(u=>s.Contains(u.id)); 等同于sql语 ...
- MongoDB中的group
在Mongodb的查询中,有类似于SQL中group by功能的group函数.两者的功能有些类似,但是区别也是比较明显的. 对于SQL来说,group by的作用就是安装依据列来将数据表中的记录分成 ...
- Pandas 分组聚合 :分组、分组对象操作
1.概述 1.1 group语法 df.groupby(self, by=None, axis=0, level=None, as_index: bool=True, sort: bool=True, ...
- MySQL进阶5--分组函数 / 分组排序和分组查询 group by(having) /order by
MySQL进阶--分组排序和分组查询 group by(having) /order by /* 介绍分组函数 功能:用做统计使用,又称为聚合函数或组函数 1.分类: sum, avg 求和 /平均数 ...
- SQL语句(三)分组函数和分组查询
目录 一.分组函数 特点 1. 各函数的简单使用 2. 搭配distinct的使用 3. COUNT 统计行数 4. 和分组函数一同查询的字段要求是group by后的字段 二.分组查询 1. 简单应 ...
- Django之无名分组,有名分组
在Django 2.0版本之前,在urls,py文件中,用url设定视图函数 urlpatterns = [ url(r'login/',views.login), ] 其中第一个参数是正则匹配,如下 ...
- 在mysql中使用group by和order by取每个分组中日期最大一行数据
转载自:https://blog.csdn.net/shiyong1949/article/details/78482737 在mysql中使用group by进行分组后取某一列的最大值,我们可以直接 ...
随机推荐
- AttributeError: module ‘select’ has no attribute 'epoll’
场景:mac 下导入的 ‘select’ 包 import select,然后在 主函数 中创建的 epoll 对象 epl = select.epoll(),运行报错如下 Traceback (mo ...
- mybatis ResultHandler vs ResultSetHandler及自定义扩展
ResultSetHandler是mybatis的关键类之一,用于对jdbc返回的ResultSet进行映射处理,其中包括列前缀处理,逻辑分页,鉴别器(Discriminator,基于值实现动态映射列 ...
- jetty源码下载
jetty下载地址:https://www.eclipse.org/jetty/download.html Release 9.4.20.v20190813 .zip .tgz api ...
- 面试突击(五)——Java常用集合
为了勾起回忆,我画了一个常用集合类的结构关系图,话不多说,详见下图: 实际开发中ArrayList/HashMap/HashSet是三种最常用的集合工具类,通过其结构关系图也能清晰的了解他们的特性,所 ...
- iobit-unlocker --- 类似 Unlocker 工具,强制删除文件或文件夹
win10 使用 Unlocker 1.9.2 常有问题,以前在win7上使用完全ok的 更换成:iobit-unlocker ,win10体验还可以,类似Unlocker 下载地址: https:/ ...
- scrapy爬虫案例:用MongoDB保存数据
用Pymongo保存数据 爬取豆瓣电影top250movie.douban.com/top250的电影数据,并保存在MongoDB中. items.py class DoubanspiderItem( ...
- Vue ElementUI主页面搭建和导航栏使用,并在刷新页面的时候选中状态消失的问题解决
<template> <div style="height:100%;width: 100%; padding:0 auto; margin: 0 auto;"& ...
- PHP环境搭建-Windows系统下PHP环境搭建
1.PHP环境搭建的前提是 Apache HTTP Server (Apache 服务器)已经安装部署成功,并可以正常访问到服务器的主页面.Apache HTTP Server 的安装部署已经在上一篇 ...
- Elasticsearch学习笔记——索引模板
在索引模板里面,date类型的字段的format支持多种类型,在es中全部会转换成long类型进行存储,参考 https://zhuanlan.zhihu.com/p/34240906 一个索引模板范 ...
- java https post请求并忽略证书,参数放在body中
1 新建java类,作用是绕过证书用 package cn.smartercampus.core.util; import java.security.cert.CertificateExceptio ...