英文:
Spring/MongoDB Multi count for unique query
问题
这是您要翻译的内容:
我必须将此查询翻译为计算每个类别的所有公共和私有库的数量:
"$group" : {
_id:"$category",
nbPublic:{ $sum: { $cond: { if: "$public", then: 1, else: 0 }}},
nbPrivate:{ $sum: { $cond: { if: "$public", then: 0, else: 1 }}}
}
以获得以下类型的结果:
{ cat1: { nbPublic: 3, nbPrivate: 5 } },
{ cat2: { nbPublic: 12, nbPrivate: 3 } },
....
我尝试过:
Aggregation aggregation = newAggregation(
group("category").count().as("nbPublic"),
match(Criteria.where("public").is(true)),
group("category").count().as("nbPrivate"),
match(Criteria.where("public").is(false)),
project("nbPublic").and("category").previousOperation().and("nbPrivate").previousOperation());
AggregationResults<Dest.class> result = mongoTemplate.aggregate(
aggregation, Model.class, Dest.class
);
我应该怎么做?
谢谢您的帮助
英文:
I have to translate this query to count all public and private libaries for each category:
"$group" : {
_id:"$category",
nbPublic:{ $sum: { $cond: { if: "$public", then: 1, else: 0 }}},
nbPrivate:{ $sum: { $cond: { if: "$public", then: 0, else: 1 }}}
}
to get that type of result :
{ cat1: { nbPublic: 3, nbPrivate: 5 } },
{ cat2: { nbPublic: 12, nbPrivate: 3 } },
....
I tried :
Aggregation aggregation = newAggregation(
group("category").count().as("nbPublic"),
match(Criteria.where("public").is(true)),
group("category").count().as("nbPrivate"),
match(Criteria.where("public").is(false)),
project("nbPublic").and("category").previousOperation().and("nbPrivate").previousOperation());
AggregationResults<Dest.class> result = mongoTemplate.aggregate(
aggregation, Model.class, Dest.class
);
What should I do ?
Thx for your help
答案1
得分: 0
你可以使用mongoTemplate
来实现这个功能。
@Autowired
MongoTemplate mongoTemplate;
然后你可以像这样进行操作。
public List<Object> test() {
Aggregation aggregation = Aggregation.newAggregation(
a -> new Document("$group",
new Document("_id", "$category")
.append("nbPublic",
new Document("$sum",
new Document("$cond",
new Document("$if", "$public")
.append("then", 1)
.append("else", 0)
)
)
)
.append("nbPrivate",
new Document("$sum",
new Document("$cond",
new Document("$if", "$public")
.append("then", 0)
.append("else", 1)
)
)
)
)
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
}
注意:我没有测试过。但是这应该能够工作。这是一种将Mongo脚本转换为Spring的“技巧”。无论如何,我会查找并告诉你关于聚合操作的信息。
更新 1:这可能是简单的方法
group("category")
.sum(ConditionalOperators.when("public").then(1).otherwise(0)).as("nbPublic")
.sum(ConditionalOperators.when("public").then(0).otherwise(1)).as("nbPrivate")
英文:
You can achieve this with mongoTemplate
@Autowired
MongoTemplate mongoTemplate;
Then you can do something like this.
public List<Object> test(){
Aggregation aggregation = Aggregation.newAggregation(
a-> new Document("$group",
new Document("_id","$category")
.append("nbPublic",
new Document("$sum",
new Document ("$cond",
new Document("$if", "$public")
.append("then",1)
.append("else",0)
)
)
)
.append("nbPrivate",
new Document("$sum",
new Document ("$cond",
new Document("$if", "$public")
.append("then",1)
.append("else",0)
)
)
)
)
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
}
Note : I've not tested. But this should work. this is a kind of trick to convert mongo script to spring. Anyhow I'll find and let you know about the Aggregation operations.
Update 1 : This may be easy way
group("category")
.sum(ConditionalOperators.when("public").then(1).otherwise(0)).as("nbPublic")
.sum(ConditionalOperators.when("public").then(0).otherwise(1)).as("nbPrivate")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论