我正在尝试使用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])
这是我写的代码.
AggregateIterableiterable = 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}}
你能解释一下哪个部分错了吗?
所以,最后,这段代码返回了我想要的结果.
AggregateIterableiterable = 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))));