在汇总中按大小写排序

时间:2018-08-07 15:18:59

标签: spring-data-mongodb

我正在尝试使用不区分大小写的选项对结果进行排序。所以这是我的代码:

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

但是当我使用聚合执行查询时,它不起作用:

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

我在调试控制台中显示了查询,并且ignoreCase在聚合中完全消失了:

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

如何将忽略大小写选项放入聚合中?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

解决方案是使用MongoDB Collation并将其添加到mongoOperations中。 我使用strength = 1忽略大小写。

答案 1 :(得分:0)

这就是我解决排序问题的方式

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