0
我想用spring在mongo模板中寫這個聚合查詢。如何在春天在mongo模板中寫這個聚集查詢
這是我的查詢:
db.getCollection('CANdata_fc_distance_report').aggregate(
{$match: { device_datetime : { $gte :1462041000000, $lte: 1462732200000 }}},
{"$group" : {_id:{fc :"$fc",vehicle_name:"$vehicle_name",
device_id : "$device_id"},
count:{$sum:1}}}
)
這是上面查詢的結果
/* 1 */
{
"_id" : {
"fc" : NumberLong(1),
"vehicle_name" : "WPD 9020",
"device_id" : NumberLong(157)
},
"count" : 2
}
/* 2 */
{
"_id" : {
"fc" : NumberLong(2),
"vehicle_name" : "VVD 8966",
"device_id" : NumberLong(137)
},
"count" : 1
}
這是我在表中的數據:
/* 1 */
{
"_id" : ObjectId("581829855d08921ee6f0ac39"),
"_class" : "com.analysis.model.mongo.fc_distance_report",
"device_id" : NumberLong(137),
"vehicle_name" : "VVD 8966",
"distance" : 125.01,
"fc" : NumberLong(1),
"device_datetime" : NumberLong(1462041000000)
}
/* 2 */
{
"_id" : ObjectId("581830335d08921ee6f0ad6b"),
"_class" : "com.analysis.model.mongo.fc_distance_report",
"device_id" : NumberLong(137),
"vehicle_name" : "VVD 8966",
"distance" : 171.88,
"fc" : NumberLong(2),
"device_datetime" : NumberLong(1462127400000)
}
我發現例如我谷歌搜索添加了匹配標準,但我不知道如何編寫3列的分組
Aggregation agg = newAggregation(match(Criteria.where("device_datetime").exists(true)
.andOperator(
Criteria.where("device_datetime").gte(startDate),
Criteria.where("device_datetime").lte(endDate))),
group("hosting").count().as("total"),
project("total").and("hosting").previousOperation(),
sort(Sort.Direction.DESC, "total")
);
請幫幫我。謝謝