2017-06-27 6 views
0

mongodbクエリを書きました。そして、集団クラスを使って春のブートでそれを変換する間にいくつかの問題に直面している。だから、私はそれを集約クラスを使用して春のブートでそれを変換することを助けてください。以下は集計クラスを使用して既存のmongo dbクエリをスプリングブートに変換するには

db.api_audit.aggregate([{ 

    $match: { 
     merchant_id: '015994832961', 
     request_time: {$gte: ISODate("2017-05-11T00:00:00.0Z"), 
      $lt: ISODate("2017-05-12T00:00:00.0Z")}}}, 
{ 
    $group: 
    { 
     _id: { 
     SERVICE_NAME: "$service_name", 

     STATUS: "$status" 
    }, 
    count: { 
     "$sum": 1 
    } 
} 
}, { 


$group: { 
    _id: "$_id.SERVICE_NAME", 

    STATUSCOUNT: { 
     $push: { 
      Service_name: "$_id.STATUS", 
      count: "$count" 
     } 
    } 
} 
}, 
{ $sort : { "STATUSCOUNT.count" : -1} } 
    ]) 

デシベルのクエリ応答は、事前に

{ 
"_id" : "sendOTP", 
"STATUSCOUNT" : [ 
    { 
     "status" : "SUCCESS", 
     "count" : 2.0 
    } 
] 
} 

おかげです。

答えて

1

まず、必要な操作をすべて作成してから、それらを集計パイプラインに追加します。次に、それをautowired mongotemplateに送ります。

このような何か:

@Autowired 
private final MongoTemplate mongoTemplate; 

void aggregate() 
{ 

    Criteria match = where("merchant_id").is("015994832961").andOperator(where("request_time").gte(Date.parse("2017-05-11T00:00:00.0Z")).lt(Date.parse("2017-05-11T00:00:00.0Z"))); 
    GroupOperation groupOperation1 = group(fields().and("SERVICE_NAME").and("STATUS")).count().as("count"); 
    GroupOperation groupOperation2 = ...//(not sure how push works here, but it should not be hard to figure out) 
    SortOperation sortOperation = sort(DESC, "STATUSCOUNT.count"); 

    Aggregation aggegation = Aggregation.newAggregation(match, groupOperation1, groupOperation2, sortOperation); 

    List<Result> results = mongoTemplate.aggegate(aggregation, ObjectOfCollectionToRunOn.class, Result.class).getMappedResults(); 
} 
関連する問題