【mongodb】--自定义排序规则

目录

一、说明

最近项目接到一个功能点,需要对状态值status字段按照规则排序。这个status在表存储的是String纯字母,另外排序要求又不能按照字典排序方法。那这种问题如何解决?

MongoDB暂时只支持按照某些字段的升序或者降序排列。但是,在某些特别场景下, 比如对中文有要求按照指定规则排序,此时就是MongoDB的自定义排序规则。

二、代码案例实现

通过集合方式来自定义查询

java 复制代码
       Criteria criteria = new Criteria();
        criteria.and("sid").is(000000L);
        criteria.and("file_type").in(Arrays.asList("PPTX", "DOCX"));

        List<AggregationOperation> operations = new ArrayList<>();
        Fields fields = Fields.fields("id","create_time","update_time","status");
        // 添加查询条件
        operations.add(Aggregation.match(criteria));
        if("desc".equals(order)){
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("notStarted")).then("1").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("going")).then("2").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("success")).then("3").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("fail")).then("4").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields).and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("error")).then("5").otherwiseValueOf("status")));

operations.add(Aggregation.sort(Sort.by(Sort.Direction.DESC, "update_time","status")));
}else{
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("notStarted")).then("5").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("going")).then("4").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("success")).then("3").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("fail")).then("2").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("error")).then("1").otherwiseValueOf("status")));
            operations.add(Aggregation.sort(Sort.by(Sort.Direction.ASC, "update_time","status")));
        }

        // 设置分页参数
        operations.add(Aggregation.skip((page-1)*size));
        operations.add(Aggregation.limit(size));
        // 根据上面自定义的排序规则将对应的数据转换回来
        if("desc".equals(order)){
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("1")).then("notStarted").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("2")).then("going").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("3")).then("success").otherwiseValueOf("status")));
            operations.add(Aggregation.project(fields)
                    .and("astatus").applyCondition(ConditionalOperators.when(Criteria.where("status").is("4")).then("fail").otherwiseValueOf("status")));
          
            operations.add(Aggregation.project(fields)
                    .and("status").applyCondition(ConditionalOperators.when(Criteria.where("status").is("5")).then("error").otherwiseValueOf("status")));
          
        }else{
            //todo  类似
        }

        Aggregation aggregation = Aggregation.newAggregation(operations);
        AggregationResults<JSONObject> aggregationResults = mongoTemplate.aggregate(aggregation, "test_record", JSONObject.class);
        List<JSONObject> templateList = aggregationResults.getMappedResults();
      
相关推荐
云和数据.ChenGuang2 天前
openEuler系统下安装MongoDB的技术教程
运维·数据库·mongodb·压力测试·运维工程师·运维技术
ChristXlx2 天前
Linux安装MongoDB(虚拟机适用)
linux·mongodb·postgresql
2301_796512522 天前
React Native鸿蒙跨平台开发如何使用MongoDB或Firebase作为后端数据库来存储车辆信息、保养记录和预约信息
数据库·mongodb·react native
数据与人3 天前
mongodb报错Sort exceeded memory limit of 104857600 bytes
数据库·mongodb
赵渝强老师3 天前
【赵渝强老师】MongoDB的数据类型
数据库·mongodb·nosql
济南java开发,求内推3 天前
MongoDB: 升级版本至:5.0.28
数据库·mongodb
wusp19944 天前
基于vite + nodejs + MongoDB + vue2 的博客发布系统
数据库·mongodb
这儿有一堆花4 天前
JSON 与 MongoDB:直存对象的便利与隐性代价
数据库·mongodb·json
嫂子的姐夫5 天前
py连接MongoDB
数据库·爬虫·mongodb
小毅&Nora5 天前
【后端】【数据库】MongoDB存储引擎选型指南:WiredTiger如何用B+树吊打B-树
数据库·mongodb·存储引擎