Search code examples
javaaggregation-frameworkspring-data-mongodbspring-mongo

Specify an Index on a Spring Data TypedAggregation


I'm using Spring Data Mongo and one of our aggregations are using the wrong index. I'm using :

  • spring-data-mongodb v 1.10.11.RELEASE
  • mongo driver 3.6.4
  • JAVA 1.8

A simplified version of the code would be something like this :

Criteria criteria = createMatch("user", "email");
AggregationOperation userCriteria  = Aggregation.match(criteria);
TypedAggregation<NucleusOccupancySummary> aggregation = Aggregation.newAggregation(User.class,userCriteria);
AggregationResults<NucleusOccupancySummary> aggregationResults = mongoOperations.aggregate(aggregation, User.class, User.class);

In this case the aggregation is selecting the wrong index. I have been able to get it to use the correct index by adding a sort to the query, but this seems like a sloppy way of accomplishing this.

I'm wondering if anyone has had any success with using aggregation or cursor hints (or any other way) to specify the index that should be used for aggregation.


Update : As of Spring Data MongoDB 3.1 hinting for aggregations are supported by providing a BSON document as the hint on the AggregationOptions.Builder.


Solution

  • No there is no way to pass the hint to the aggregation query in the spring even though the mongo java driver (AggregateIterable) supports it.

    Currently AggregationOptions supports batchSize, cursor, explain, allowDiskUse and collation.

    There is a jira pending to add hint to the aggregation. You can follow up there.