繁体   English   中英

在 Spring 数据 Mongodb 中使用查询 class 进行不区分大小写的排序

[英]Case-insensitive sort using Query class in Spring Data Mongodb

我正在使用这种类型的排序,并且我希望不区分大小写。

            Query query = new Query();
            query.with(new Sort(new Order(Sort.Direction.ASC,"title").ignoreCase()));
            return db.find(query, Video.class);

我尝试了这个查询,但没有得到任何结果。

使用的进口:

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

例如,如果我有这种类型的标题:“Inception”、“BlackList”、“adore”、“123”、“city”、“desperadoS”

顺序应该是:"123","adore","BlackList","city","desperadoS","Inception"

如果我以这种方式使用它

        Query query = new Query();
        query.with(new Sort(Sort.Direction.ASC,"title"));
        return db.find(query, Video.class);

它返回

《123》、《黑名单》、《盗梦空间》、《爱恋》、《城市》、《亡命之徒》

spring-data-mongodb 1.9.2版

使用排序规则对忽略的情况进行排序。 例子在这里

使用new Order(Sort.Direction.ASC,"title").ignoreCase())时,您将收到 IllegalArgumentException

在你的情况下:

Query query = new Query().with(Sort.by(new Sort.Order(Sort.Direction.ASC, "title")));
query.collation(Collation.of("en").strength(Collation.ComparisonLevel.secondary()));
return mongoTemplate.find(query, Video.class);

另一种使用Collation ion 和@Query注释的方法来获得"123","adore","BlackList","city","desperadoS","Inception"的预期排序顺序

文档

@Document(collection = "video")
@Data
public class Video {

    @Id
    private String id;
    private String title;

    public Video(String title) {
        this.title = title;
    }
}

存储库

@Repository
public interface VideoRepository extends MongoRepository<Video, String> {

    @Query(collation = "en", value = "{}")
    List<Video> getAllSortedVideos(Sort sort);

}

集成测试 class 以断言更改

@ActiveProfiles("test")
@SpringBootTest(classes = DemoApplication.class, 
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class VideoRepositoryITest {

    @Autowired
    private VideoRepository videoRepository;

    @Test
    void getAllSortedVideos() {
        List<String> expectedSystemNamesInOrder = Arrays.asList("123", "adore", "BlackList",
                                                    "city", "desperadoS", "Inception");
        //breaking the order for fun
        Set<String> expectedSystemNamesSet = new HashSet<>(expectedSystemNamesInOrder);
        //saving the videos of each title
        expectedSystemNamesSet.stream().map(Video::new)
                .forEach(videoRepository::save);
        //fetching sorted Videos by title
        List<Video> videos = videoRepository.getAllSortedVideos(Sort.by(Direction.ASC, "title"));
        //fetching sorted Video tiles to assert
        List<String> titles = videos.stream().map(Video::getTitle).collect(Collectors.toList());
        //asserting the result video title order with the expected order
        for (int i = 0; i < titles.size(); i++) {
            String actualTitle = titles.get(i);
            String expectedTitle = expectedSystemNamesInOrder.get(i);
            //Test case will fail if the retrieved title order doesn't match with expected order
            Assertions.assertEquals(expectedTitle, actualTitle);
        }
    }

}

使用org.springframework.data:spring-data-mongodb:3.0.4.RELEASE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM