我正在尝试使用mongodb java驱动程序通过聚合函数编写组.这是数据库的文档结构.{ _id : ObjectId(58819bd9f16a7802523bc077), Date : 12/19/2016, Time : 4:15:00, Temperature : 65.5, User : A...
我正在尝试使用mongodb java驱动程序通过聚合函数编写组.
这是数据库的文档结构.
{ "_id" : ObjectId("58819bd9f16a7802523bc077"), "Date" : "12/19/2016", "Time" : "4:15:00", "Temperature" : 65.5, "User" : "A", "ThermalComfort" : -1, "settingID" : ObjectId("58819bd6f16a7802523bbdc5") }
{ "_id" : ObjectId("58819bd9f16a7802523bc078"), "Date" : "12/19/2016", "Time" : "4:30:00", "Temperature" : 65.25, "User" : "A", "ThermalComfort" : -1, "settingID" : ObjectId("58819bd6f16a7802523bbdc5") }
我想按“日期”和“时间”分组,仅投影ThermalComfort.我的预期输出为:{date = 12/19/16,time = 0:00:00,listOfTC = [2,1]}. listOfTC是由Array中的每个读数组成的列表:(例如[-1,-1])
这是我写的代码.
AggregateIterable<Document> iterable = themalComfortProfileCollection.aggregate(Arrays.asList(
new Document("$group", new Document("_id", "$Date" + "$Time").append("count", new Document("$sum", 1))),
new Document("$project", new Document("comfortIndex", "$ThermalComfort"))));
但是,如果我打印出文档,我将获得null文档.
for (Document doc : iterable) {
System.out.println(doc);
}
Document{{_id=null}}
你能解释一下哪个部分错了吗?
解决方法:
所以,最后,这段代码返回了我想要的结果.
AggregateIterable<Document> iterable = themalComfortProfileCollection.aggregate(Arrays.asList(
new Document("$group", new Document("_id", new Document("date", "$Date").append("time", "$Time") ).append("ThermalComfortList", new Document("$push", "$ThermalComfort"))),
new Document("$sort", new Document("_id", 1))));
编程基础网
本文标题为:mongodb java驱动程序聚合组
基础教程推荐
猜你喜欢
- 从Java调用SQL Server中的表变量的问题 2023-10-31
- java – 通过JDBC处理不同数据库方言的模式 2023-10-29
- java – 从Gradle,Spring和DB2开始的挑战 2023-10-30
- java – Tomcat 8中DB2的Log4j jdbc appender 2023-10-30
- 使用@CacheEvict 多参数如何匹配删除 2023-08-10
- 基于Java实现简易的七星彩号码生成器 2023-04-06
- Spring Bean生命周期详细分析 2023-04-12
- Java Vector实现班级信息管理系统 2022-11-03
- 一文带你学会Java网络编程 2023-04-06
- 如何使用离线和在线数据库创建Java桌面应用程序,定期同步? 2023-10-28
