2016-11-01 9 views
0

私はこの集約クエリを春を使って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) 
} 

私は私の例を見つけましたGoogleの検索で一致条件が追加されましたが、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") 
    ); 

私を助けてください。ありがとうございます

答えて

1

このようなことを試すことができます。ちょうど一緒にグループキー。

Aggregation agg = newAggregation(match(Criteria.where("device_datetime").exists(true) 
     .andOperator(
      Criteria.where("device_datetime").gte(startDate), 
      Criteria.where("device_datetime").lte(endDate))), 
    group(Fields.fields().and("fc", "$fc").and("vehicle_name", "$vehicle_name").and("device_id", "$device_id")) 
    .count().as("count"));