1
[
   {
      "categories":{
         "category":[
            "sub1",
            "sub2",
            "sub3"
         ],
         "category2":[
            "sub1",
            "sub2"
         ]
      }
   },
   {
      "categories":{
         "category":[
            "sub1",
            "sub4"
         ],
         "category2":[
            "sub5",
            "sub4"
         ]
      }
   }
]

want to get the distinct category and category2 using Spring data. I have tried both Aggregation and Distinct both slow on performance i hope i am missing something

Aggregation:

AggregationResults<Category> results = mongoTemplate.aggregate(Aggregation.newAggregation(Aggregation.group(categoryFields.stream().toArray(String[]::new))),"recipe",Category.class );

Distinct:

 categoryFields.stream().forEach(cat -> {
            categories.put(cat, mongoTemplate.findDistinct(query, "categories." + cat, "recipe", Recipe.class, Category.class));
        });

Aggregation taking 11sec and Distinct taking 3 secs both slow on performance side .

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
sankar
  • 268
  • 2
  • 15

0 Answers0