Anna
Anna

Reputation: 889

Sort with ignore case in an aggregation

I am trying to sort my result with the case insensitive option. So here is my code:

List<AggregationOperation> operations = new ArrayList<>();   
Sort.Order sort = ....ignoreCase();
operations.add(new SortOperation(new Sort(sort)));

But when I execute my query using an aggregation, it doesn't work:

mongoOperations.aggregate(Aggregation.newAggregation(operations).withOptions(aggregationOptions)

I displayed the query in the debug console and the ignoreCase totally disappeared in the aggregation:

db.getCollection('persons').aggregate([   
    {  
      "$sort":{  
        "buyer.organization.name":-1
      }
    }
])

How can I put the ignore case option in my aggregation?

Thanks for your help!

Upvotes: 1

Views: 2008

Answers (2)

Rodolfo Velasco
Rodolfo Velasco

Reputation: 855

This is how I solved my sorting issue

val res =
          mongoTemplate.aggregate(
              aggregations.withOptions(
                  new AggregationOptions(
                      false,
                      false,
                      null,
                      Collation.of("en").strength(Collation.ComparisonLevel.primary()))),
              Path.class,
              CountedAggregationFolderContentDTO.class);

Upvotes: 0

Anna
Anna

Reputation: 889

The solution for this was using MongoDB Collation and add it in the mongoOperations. I used strength = 1 which ignores cases.

Upvotes: 2

Related Questions