在聚合中忽略大小写排序
Sort with ignore case in an aggregation
我正在尝试使用不区分大小写的选项对结果进行排序。所以这是我的代码:
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
}
}
])
如何在聚合中添加忽略大小写选项?
感谢您的帮助!
这个问题的解决方案是使用 MongoDB Collation 并将其添加到 mongoOperations
中。
我使用 strength = 1
忽略大小写。
这就是我解决排序问题的方法
val res =
mongoTemplate.aggregate(
aggregations.withOptions(
new AggregationOptions(
false,
false,
null,
Collation.of("en").strength(Collation.ComparisonLevel.primary()))),
Path.class,
CountedAggregationFolderContentDTO.class);
我正在尝试使用不区分大小写的选项对结果进行排序。所以这是我的代码:
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
}
}
])
如何在聚合中添加忽略大小写选项?
感谢您的帮助!
这个问题的解决方案是使用 MongoDB Collation 并将其添加到 mongoOperations
中。
我使用 strength = 1
忽略大小写。
这就是我解决排序问题的方法
val res =
mongoTemplate.aggregate(
aggregations.withOptions(
new AggregationOptions(
false,
false,
null,
Collation.of("en").strength(Collation.ComparisonLevel.primary()))),
Path.class,
CountedAggregationFolderContentDTO.class);