0

この集計クエリを、Spring を使用して 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")
    );

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

4

1 に答える 1

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"));
于 2016-11-01T13:52:17.690 に答える