spring数据mongodb:按域名汇总
spring data mongodb: sum aggregation by domain name
我有一个包含 id, domain 的集合。在集合中,同一域保存了多次。我想聚合并得到像
这样的结果
google.com 4
times.com 5
我的代码
public List<DomainDTO> domainAggregation() {
Aggregation pipeline = newAggregation(
group(fields("id","domain")),
group("domain").count().as("count"),
sort(Sort.Direction.DESC, previousOperation(), "domain")
);
AggregationResults groupResults = mongoTemplate.aggregate(
pipeline, Domains.class, DomainDTO.class);
List<DomainDTO> domainReport = groupResults.getMappedResults();
return domainReport;
}
DomainDTO 组成
private String domain;
private Integer count;
域实体组成
private String id;
private String searchId;
private String domain;
private Date searchDate;
private String searchName;
private Integer count;
结果json是
{
"domain": null,
"count": 2
},
{
"domain": null,
"count": 1
},
{
"domain": null,
"count": 2
},
{
"domain": null,
"count": 48
},
域名未通过,未排序。找不到错误。有什么建议吗?
您当前的查询输出类似于
{ "$group" : {
"_id" : { "id" : "$id" , "domain" : "$domain"}
} } ,
{ "$group" : { "_id" : "$_id.domain" , "count" : { "$sum" : 1}}} ,
{ "$sort" : { "_id" : -1 , "_id.domain" : -1}}
我相信你打算
{ "$group" : { "_id" : "$domain" , "count" : { "$sum" : 1}}} ,
{ "$sort" : { "_id" : -1}}
聚合Java代码:
Aggregation pipeline = newAggregation(
group("domain").count().as("count"),
sort(Sort.Direction.DESC, previousOperation())
);
您将需要一个 $project
阶段来将 _id 映射回 DomainDTO
class.
中的 domain
Aggregation pipeline = newAggregation(
group("domain").count().as("count"),
sort(Sort.Direction.DESC, previousOperation()),
project(bind("domain", "_id")).andExclude("_id").andInclude("count")
);
Mongo Shell
{ "$group" : { "_id" : "$domain" , "count" : { "$sum" : 1}}} ,
{ "$sort" : { "_id" : -1}},
{ "$project" : { "domain" : "$_id" , "_id" : 0 , "count" : 1}
我有一个包含 id, domain 的集合。在集合中,同一域保存了多次。我想聚合并得到像
这样的结果google.com 4 times.com 5
我的代码
public List<DomainDTO> domainAggregation() {
Aggregation pipeline = newAggregation(
group(fields("id","domain")),
group("domain").count().as("count"),
sort(Sort.Direction.DESC, previousOperation(), "domain")
);
AggregationResults groupResults = mongoTemplate.aggregate(
pipeline, Domains.class, DomainDTO.class);
List<DomainDTO> domainReport = groupResults.getMappedResults();
return domainReport;
}
DomainDTO 组成
private String domain;
private Integer count;
域实体组成
private String id;
private String searchId;
private String domain;
private Date searchDate;
private String searchName;
private Integer count;
结果json是
{
"domain": null,
"count": 2
},
{
"domain": null,
"count": 1
},
{
"domain": null,
"count": 2
},
{
"domain": null,
"count": 48
},
域名未通过,未排序。找不到错误。有什么建议吗?
您当前的查询输出类似于
{ "$group" : {
"_id" : { "id" : "$id" , "domain" : "$domain"}
} } ,
{ "$group" : { "_id" : "$_id.domain" , "count" : { "$sum" : 1}}} ,
{ "$sort" : { "_id" : -1 , "_id.domain" : -1}}
我相信你打算
{ "$group" : { "_id" : "$domain" , "count" : { "$sum" : 1}}} ,
{ "$sort" : { "_id" : -1}}
聚合Java代码:
Aggregation pipeline = newAggregation(
group("domain").count().as("count"),
sort(Sort.Direction.DESC, previousOperation())
);
您将需要一个 $project
阶段来将 _id 映射回 DomainDTO
class.
domain
Aggregation pipeline = newAggregation(
group("domain").count().as("count"),
sort(Sort.Direction.DESC, previousOperation()),
project(bind("domain", "_id")).andExclude("_id").andInclude("count")
);
Mongo Shell
{ "$group" : { "_id" : "$domain" , "count" : { "$sum" : 1}}} ,
{ "$sort" : { "_id" : -1}},
{ "$project" : { "domain" : "$_id" , "_id" : 0 , "count" : 1}