[
{
"categories":{
"category":[
"sub1",
"sub2",
"sub3"
],
"category2":[
"sub1",
"sub2"
]
}
},
{
"categories":{
"category":[
"sub1",
"sub4"
],
"category2":[
"sub5",
"sub4"
]
}
}
]
想要使用Spring数据获取不同的category和category2。我已经尝试了“聚合”和“不同”两者的性能降低,希望我能缺少一些东西
汇总:
AggregationResults<Category> results = mongoTemplate.aggregate(Aggregation.newAggregation(Aggregation.group(categoryFields.stream().toArray(String[]::new))),"recipe",Category.class );
不同:
categoryFields.stream().forEach(cat -> {
categories.put(cat, mongoTemplate.findDistinct(query, "categories." + cat, "recipe", Recipe.class, Category.class));
});
在性能方面,聚合耗时11秒钟,而Distinct耗时3秒钟都很慢。