Spring mongo 聚合 returns 空列表
Spring mongo agregation returns empty List
我的集合中有 160.000 条数据,我必须根据字段显示有关此数据的一些指标。所以我想我可以根据我的 "domain" 字段对这些数据进行分组,并从 db.
中获取汇总数据
我为分组数据和 运行 创建了一个自定义存储库和模型,但结果始终为空,但是如果我在 robo3t 上使用记录的管道,我会得到我想要的。
感谢任何帮助。
//customRepository
@Service
public class EventRecordRepositoryCustomImpl implements EventRecordRepositoryCustom {
private final MongoTemplate mongo;
@Autowired
public EventRecordRepositoryCustomImpl(MongoTemplate mongo) {
this.mongo = mongo;
}
@Override
public List<EventRecordCounts> query(String[] statuses) {
//MatchOperation match = Aggregation.match(new Criteria("status").not().in(Arrays.asList(statuses)));
GroupOperation group = Aggregation.group("domain").sum("relatedRecordCount").as("totalRelatedEventCount").count().as("totalMainEventCount");
AggregationOptions aggregationOptions = Aggregation.newAggregationOptions().explain(true).build();
Aggregation aggregation = Aggregation.newAggregation( group).withOptions(aggregationOptions);
AggregationResults<EventRecordCounts> result = mongo.aggregate(aggregation, mongo.getCollectionName(EventRecord.class), EventRecordCounts.class);
return result.getMappedResults();
}
}
//my collection: all getter and setters are provided
@Document(collection = "event_record")
public class EventRecord extends BaseModel {
private ObjectId collectorId;
@Indexed
private String recordNumber;
@Indexed
private String status;
private Long createdAt;
@Indexed
private Long updatedAt;
private Long finishedAt;
private String responsible;
private String responsibleUserName;
private String group;
private String type;
private String importance;
private String summary;
private Integer relatedRecordCount;
@Indexed
private String domain;
}
//Expected output model: all getter and setters are provided
public class EventRecordCounts {
@Field("_id")
private String _id;
private String domain;
private long openMainEventCount;
private long openRelatedEventCount;
private long totalMainEventCount;
private long totalRelatedEventCount;
}
将记录器切换到调试级别,它只记录如下。
2019-06-21 17:46:09,182 DEBUG o.s.data.mongodb.core.MongoTemplate - Executing aggregation: { "aggregate" : "event_record" , "pipeline" : [ { "$group" : { "_id" : "$domain" , "totalRelatedEventCount" : { "$sum" : "$relatedRecordCount"} , "totalMainEventCount" : { "$sum" : 1}}}] , "explain" : true}
如果我在 robo3t 中使用管道,结果也是预期的
经过一些调试调查后,我发现了一个愚蠢的解决方案。我在 AggregationResults.rawResults
中使用键 "firstBatch" 获取数据,在 MongoTemplate
class 中使用 returnPotentiallyMappedResults
方法尝试使用 "result" 键获取数据。所以我写了一个 CustomMongoTemplate 和 ovverride returnPotentiallyMappedResults
我的集合中有 160.000 条数据,我必须根据字段显示有关此数据的一些指标。所以我想我可以根据我的 "domain" 字段对这些数据进行分组,并从 db.
中获取汇总数据我为分组数据和 运行 创建了一个自定义存储库和模型,但结果始终为空,但是如果我在 robo3t 上使用记录的管道,我会得到我想要的。
感谢任何帮助。
//customRepository
@Service
public class EventRecordRepositoryCustomImpl implements EventRecordRepositoryCustom {
private final MongoTemplate mongo;
@Autowired
public EventRecordRepositoryCustomImpl(MongoTemplate mongo) {
this.mongo = mongo;
}
@Override
public List<EventRecordCounts> query(String[] statuses) {
//MatchOperation match = Aggregation.match(new Criteria("status").not().in(Arrays.asList(statuses)));
GroupOperation group = Aggregation.group("domain").sum("relatedRecordCount").as("totalRelatedEventCount").count().as("totalMainEventCount");
AggregationOptions aggregationOptions = Aggregation.newAggregationOptions().explain(true).build();
Aggregation aggregation = Aggregation.newAggregation( group).withOptions(aggregationOptions);
AggregationResults<EventRecordCounts> result = mongo.aggregate(aggregation, mongo.getCollectionName(EventRecord.class), EventRecordCounts.class);
return result.getMappedResults();
}
}
//my collection: all getter and setters are provided
@Document(collection = "event_record")
public class EventRecord extends BaseModel {
private ObjectId collectorId;
@Indexed
private String recordNumber;
@Indexed
private String status;
private Long createdAt;
@Indexed
private Long updatedAt;
private Long finishedAt;
private String responsible;
private String responsibleUserName;
private String group;
private String type;
private String importance;
private String summary;
private Integer relatedRecordCount;
@Indexed
private String domain;
}
//Expected output model: all getter and setters are provided
public class EventRecordCounts {
@Field("_id")
private String _id;
private String domain;
private long openMainEventCount;
private long openRelatedEventCount;
private long totalMainEventCount;
private long totalRelatedEventCount;
}
将记录器切换到调试级别,它只记录如下。
2019-06-21 17:46:09,182 DEBUG o.s.data.mongodb.core.MongoTemplate - Executing aggregation: { "aggregate" : "event_record" , "pipeline" : [ { "$group" : { "_id" : "$domain" , "totalRelatedEventCount" : { "$sum" : "$relatedRecordCount"} , "totalMainEventCount" : { "$sum" : 1}}}] , "explain" : true}
如果我在 robo3t 中使用管道,结果也是预期的
经过一些调试调查后,我发现了一个愚蠢的解决方案。我在 AggregationResults.rawResults
中使用键 "firstBatch" 获取数据,在 MongoTemplate
class 中使用 returnPotentiallyMappedResults
方法尝试使用 "result" 键获取数据。所以我写了一个 CustomMongoTemplate 和 ovverride returnPotentiallyMappedResults