mongo java aggregate_Java中使用mongodb的aggregate聚合查询
首先,我们在数据库中,mongodb的聚合查询是这样写。
db.getCollection('parking_record').aggregate(
{$match : {"appId" : "2e1800b22ae70600", "leaveTime" : {"$gt" : ISODate("2017-07-12T00:00:00"), "$lt" : ISODate("2017-07-13T00:00:00")}}},
{$group : {"_id" : "$leaveMethod", "count" : {$sum : 1}}},
{$sort : {"_id" : 1}}
)
在java类中,应该怎样呢?这是我写的其中一个方法。
(首先要导入mongodb的java驱动包mongo-java-driver-3.2.2.jar)
/**
* 根据日期统计离场方式
* @param app_id 插件ID
* @param beginDate 开始日期
* @param endDate 结束日期
* @return {"ManualLeave":2,"AutoLeave":4}
* @throws Exception
*/
public String aggregateLeaveMethodByDate(String app_id, Date beginDate, Date endDate) throws Exception {
MongoCollection collection = PluginMongo.instance().getDatabase().getCollection(MongoCollectionName.PARKING_RECORD);
Document sub_match = new Document();
sub_match.put("appId", app_id);
sub_match.put("leaveTime", new Document("$gt", beginDate).append("$lt", endDate));
Document sub_group = new Document();
sub_group.put("_id", "$leaveMethod");
sub_group.put("count", new Document("$sum", 1));
Document match = new Document("$match", sub_match);
Document group = new Document("$group", sub_group);
Document sort = new Document("$sort", new Document("_id", 1));
List aggregateList = new ArrayList();
aggregateList.add(match);
aggregateList.add(group);
aggregateList.add(sort);
JSONObject ret_obj = new JSONObject();
AggregateIterable resultset = collection.aggregate(aggregateList);
MongoCursor cursor = resultset.iterator();
try {
while(cursor.hasNext()) {
Document item_doc = cursor.next();
int leaveMethod = item_doc.getInteger("_id", 0);
int count = item_doc.getInteger("count", 0);
LeaveMethodEnum leaveMethodVal = LeaveMethodEnum.fromType(leaveMethod);
ret_obj.put(leaveMethodVal.name(), count);
}
} finally {
cursor.close();
}
return ret_obj.toJSONString();
}上面的只有matche,group等几个常用,project,limit等类似,可以参考上面的。
aggregate的相关sql知识可以参考菜鸟教程:http://www.runoob.com/mongodb/mongodb-aggregate.html
mongo java aggregate_Java中使用mongodb的aggregate聚合查询相关推荐
- 【Elasticsearch】Elasticsearch中 aggs (桶)聚合查询和进行二次聚合查询
[Elasticsearch]Elasticsearch中 aggs (桶)聚合查询和进行二次聚合查询 Bucket aggregationsedit Bucket aggregations don' ...
- Java中mongodb指定DB通过aggregate聚合查询操作示例
目录 前言: 应用场景: 命令描述: 代码示例: 聚会查询: 数量查询: 前言: 大家都知道,mongodb是一个非关系型数据库,也就是说,mongodb数据库中的每张表是独立存在的,表与表之间没有 ...
- Java 程序中使用 MongoDB教程
环境配置 在 Java 程序中如果要使用 MongoDB,你需要确保已经安装了 Java 环境及 MongoDB JDBC 驱动. 本章节实例时候 Mongo 3.x 以上版本. 你可以参考本站的Ja ...
- mongo java aggregate_java使用spring + mongo进行Aggregation聚合查询
笔记:使用mongo聚合查询(一开始根本没接触过mongo,一点一点慢慢的查资料完成了工作需求) 需求:在订单表中,根据buyerNick分组,统计每个buyerNick的电话.地址.支付总金额以及总 ...
- MongoTemplate 使用aggregate聚合查询
db.getCollection('trade').aggregate([ // $match 在$group 前过滤分组前数据 $match在$group之后过滤分组后的文档{ $match : { ...
- mongotemplate 查list_mongoTemplate.aggregate()聚合查询 时间
一.概述 1. 聚合的表达式 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 下表展示了一些聚 ...
- mongo java mapreduce_MongoDB中的MapReduce简介
MongoDB MapReduce MapReduce是一种计算模型,简单的说就是将大批量的工作(数据)分解(MAP)执行,然后再将结果合并成最终结果(REDUCE).这样做的好处是可以在任务被分解后 ...
- mongo java mapreduce_MongoDB中MapReduce介绍与使用
一.简介 在用MongoDB查询返回的数据量很大的情况下,做一些比较复杂的统计和聚合操作做花费的时间很长的时候,可以用MongoDB中的MapReduce进行实现 MapReduce是个非常灵活和强大 ...
- Java开发中的使用redis缓存提升查询效率
枚举类 public enum AppKey {/*** Redis Key 约定*/USER_SESSION("USER"),SYS_MENU("MENU") ...
最新文章
- 不要再用arxiv链接了!为了让论文引用更规范,上交毕业生、南加州大学华人博士创建了一个小工具...
- 轻量级WEB开发框架flask
- dell笔记本电脑驱动_笔记本电脑摄像头打不开,怎么办?总结经验(基于摄像头本身是完好的)...
- 学会python的基础操作题_第3章 Python基础-文件操作函数 文件操作 练习题
- 怎样不停请求接口实现实时刷新_快狗打车实时数仓和基于Hologres的数据服务建设...
- springcloud hystrix入门简介(二)
- Leetcode每日一题:剑指offer22.lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof(链表中倒数第k个节点)
- java内存溢出模拟_模拟实战排查堆内存溢出(java.lang.OutOfMemoryError: Java heap space)问题...
- rds mysql_Amazon RDS上MySQL
- ASP.NET2.0一次发送多封邮件
- M语言中的操作符说明:数字以及时间相关的操作符
- 读书笔记202208 TRC2010 Multi-agent model predictive control of signaling split in urban traffic networks
- JAVA退房计算价格怎么写,JAVA宾馆客房管理系统报告.doc
- 《电脑商情报》国内信号最强,有效距离最远的四种无线网卡
- VisionPro工具说明
- 比亚迪唐DMI购车经验分享
- 谈谈我在创业公司的感悟
- 如何判断欠拟合、适度拟合、过拟合
- 《C语言入门经典》Ivor Horton第十章练习题3
- ISP-坏点校正(DPC)
热门文章
- Django报错:__init__() missing 1 required positional argument: 'on_delete'
- linux cgi c环境配置,Linux下配置nginx支持.cgi | 系统运维
- this.setState同步异步问题
- taro单选框、多选框当前勾选的值
- 图解最大熵原理(The Maximum Entropy Principle)
- 9宫格解锁 android_android开发图案解锁学习记录一(九宫格的绘制)
- linux文件的权限机制,linux 文件特殊权限
- Android:同步屏障的简单理解和使用
- Centos7中查看IP命令:IP addr
- 杨焘鸣:请求帮忙 开场白话术