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


    一、说明

    最近项目接到一个功能点,需要对状态值status字段按照规则排序。这个status在表存储的是String纯字母,另外排序要求又不能按照字典排序方法。那这种问题如何解决?
    MongoDB暂时只支持按照某些字段的升序或者降序排列。但是,在某些特别场景下, 比如对中文有要求按照指定规则排序,此时就是MongoDB的自定义排序规则。

    二、代码案例实现

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

           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();
          
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    Codeforces Round 929 (Div. 3)---->E. Turtle vs. Rabbit Race: Optimal Trainings
    HTML静态页面获取url参数和UserAgent
    P2802 回家
    Linux高级实战部署专题篇一:Docker入门(容器的安装部署,基本命令使用)
    jvm06
    基于C#实现字符串相似度
    一篇玩转mybatis-plus框架的详细讲解(入门必备)
    RabbitMQ(原理,下载,安装)
    使用大模型提效程序员工作
    LeetCode26.删除有序数组中的重复项(双指针法)
  • 原文地址:https://blog.csdn.net/xunmengyou1990/article/details/132801400