java collections.sort 忽略大小写排序_Java Spring Mongo排序忽略大小写问题
我正在使用Spring-Data-mongodb對MongoDB執行各種請求.
嘗試執行分頁&時用忽略的情況排序我得到一個例外,
這是我的代碼:
Sort.Order order = new Sort.Order(ascending? Sort.Direction.ASC: Sort.Direction.DESC, sortKey).ignoreCase();
Query query = new Query(filter).with(new PageRequest(page, size, new Sort(order)));
return mongoTemplate.find(query, clazz,collection);
注意在Sort.Order對象上應用的.IgnoreCase()方法.
Query .with方法失敗,并引發異常:
java.lang.IllegalArgumentException: Given sort contained an Order for lastName with ignore case! MongoDB does not support sorting ignoreing case currently!
at org.springframework.data.mongodb.core.query.Query.with(Query.java:179)
at org.springframework.data.mongodb.core.query.Query.with(Query.java:162)
如果刪除.IgnoreCase()方法,即執行以下代碼:
Sort.Order order = new Sort.Order(ascending? Sort.Direction.ASC: Sort.Direction.DESC, sortKey);
Query query = new Query(filter).with(new PageRequest(page, size, new Sort(order)));
return mongoTemplate.find(query, clazz,collection);
一切正常,除了我當然不會得到不敏感的排序結果.
因此我可能會得到A B C a1 a2而不是A a1 a2 BC.
即使該異常提到mongoDB不支持IgnoreCase排序,但我使用的是mongo 3.4,據我所知,它確實支持可分頁排序(Here’s the official JIRA issue regarding insensitive search feature added)的ignoreCase選項,而spring-data-mongodb軟件包為1.8.
解決方法:
對不起,如果我的評論不清楚.您必須使用排序查詢發送歸類.
強度主要和次要都將提供不區分大小寫的排序.確保在排序查詢中使用確切的排序規則條件以利用索引.
Sort.Order order = new Sort.Order(ascending? Sort.Direction.ASC: Sort.Direction.DESC, sortKey);
Query query = new Query(filter).with(new PageRequest(page, size, new Sort(order)));
query.collation(Collation.of("en").strength(Collation.ComparisonLevel.secondary()));
return mongoTemplate.find(query, clazz,collection);
標簽:mongodb,spring,java,spring-mvc
來源: https://codeday.me/bug/20191110/2013875.html
總結
以上是生活随笔為你收集整理的java collections.sort 忽略大小写排序_Java Spring Mongo排序忽略大小写问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 架子鼓cad平面模型(架子鼓cad平面图
- 下一篇: java简历达内_达内教你怎么写大牛简历