【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();
      
相关推荐
仰望星空的凡人8 小时前
【JS逆向基础】数据库之MongoDB
javascript·数据库·python·mongodb
巴里巴气1 天前
MongoDB事务和隔离级别的原理
数据库·mongodb
巴里巴气1 天前
MongoDB索引及其原理
数据库·mongodb
moxiaoran57532 天前
Springboot+MongoDB简单使用示例
spring boot·mongodb·spring
Wang's Blog3 天前
Nestjs框架: 基于Mongodb的多租户功能集成和优化
数据库·mongodb·多租户
鼠鼠我捏,要死了捏3 天前
深入解析MongoDB分片原理与运维实践指南
mongodb·性能优化·sharding
~央千澈~3 天前
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
数据库·mongodb
csdn_aspnet4 天前
在 Windows 上安装设置 MongoDB及常见问题
windows·mongodb
Fireworkitte5 天前
MongoDB的操作
数据库·mongodb
Fireworkitte5 天前
MongoDB常用场景
数据库·mongodb