Java 如何使用聚合类将现有的mongo db查询转换为spring引导
我编写了mongodb查询。我在使用聚合类在spring boot中转换它时遇到了一些问题。所以,请帮助我,我希望它在spring boot中使用聚合类进行转换Java 如何使用聚合类将现有的mongo db查询转换为spring引导,java,spring,mongodb,spring-boot,Java,Spring,Mongodb,Spring Boot,我编写了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-
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();
}
@Autowired
私有最终MongoTemplate MongoTemplate;
空骨料()
{
标准匹配=其中(“商户id”)为(“015994832961”)。和运营商(其中(“请求时间”).gte(日期解析(“2017-05-11T00:00:00.0Z”)).lt(日期解析(“2017-05-11T00:00.0Z”));
GroupOperation groupOperation1=组(字段()和(“服务名称”)和(“状态”).count()作为(“计数”);
GroupOperationGroupOperation2=…/(不确定推送在这里是如何工作的,但应该不难理解)
sortoOperation sortoOperation=排序(DESC,“STATUSCOUNT.count”);
Aggregation Aggregation=Aggregation.newAggregation(匹配、组操作1、组操作2、排序操作);
List results=mongoTemplate.aggegate(聚合,ObjectOfCollectionToRunOn.class,Result.class).getMappedResults();
}
首先创建所有必需的操作,然后将它们添加到聚合管道中。然后将其馈送到自动连接的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();
}
@Autowired
私有最终MongoTemplate MongoTemplate;
空骨料()
{
标准匹配=其中(“商户id”)为(“015994832961”)。和运营商(其中(“请求时间”).gte(日期解析(“2017-05-11T00:00:00.0Z”)).lt(日期解析(“2017-05-11T00:00.0Z”));
GroupOperation groupOperation1=组(字段()和(“服务名称”)和(“状态”).count()作为(“计数”);
GroupOperationGroupOperation2=…/(不确定推送在这里是如何工作的,但应该不难理解)
sortoOperation sortoOperation=排序(DESC,“STATUSCOUNT.count”);
Aggregation Aggregation=Aggregation.newAggregation(匹配、组操作1、组操作2、排序操作);
List results=mongoTemplate.aggegate(聚合,ObjectOfCollectionToRunOn.class,Result.class).getMappedResults();
}