簡體   English   中英

如何使用聚合類將現有的mongo數據庫查詢轉換為Spring Boot

[英]How can i convert my existing mongo db query in to spring boot using aggregation class

我寫了mongodb查詢。 在使用聚合類在Spring Boot中進行轉換時遇到了一些問題。所以,請幫助我,我希望它使用聚合類在Spring Boot中進行轉換。

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
    }
]
}

提前致謝。

首先,創建所有必需的操作,然后將它們添加到聚合管道。 然后,將其提供給自動裝配的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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM