如何解决如何使用Springs GroupOperation在mongodb中按字段对所有属性进行分组
我正在寻找一种表达GroupOperation的简便方法。 ProjectionOperation具有方法andInclude
String uuidRegexExp = String.format(".*%s.*",uuidSegment);
Pattern uuidPattern = Pattern.compile(uuidRegexExp);
MatchOperation match = new MatchOperation(Criteria.where("uuid").regex(uuidPattern));
SortOperation sort = Aggregation.sort(Sort.Direction.DESC,"version");
GroupOperation group = Aggregation.group("uuid")
.first("_id").as("id")
.first("version").as("version")
// Is here a shortcut like the one for the Projection with andInclude?
.first("url").as("url")
.first("host").as("host");
ProjectionOperation project = Aggregation.project()
.and("_id").as("uuid")
.and("id").as("_id")
.andInclude("url","version","host");
Aggregation aggregate = Aggregation.newAggregation(
match,sort,group,project
);
将要加载的属性应位于以下数组中:
String[] attributs = new String[]{"host","url","version"};
String uuidRegexExp = String.format(".*%s.*","version");
GroupOperation group = Aggregation.group("uuid")
.first("_id").as("id");
// Compared with the andInclude statement this isn't pretty
for (String attribute : attributs) {
group = group.first(attribute).as(attribute);
}
ProjectionOperation project = Aggregation.project()
.and("_id").as("uuid")
.and("id").as("_id")
.andInclude(attributs);
Aggregation aggregate = Aggregation.newAggregation(
match,project
);
有更好的解决方案吗? for循环可以完成任务,但效果不佳。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。