【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();
      
相关推荐
tritone11 小时前
在优豆云的免费云服务器上开启MongoDB学习之旅
服务器·学习·mongodb
小涂21 小时前
在Linux(deepin-community-25)下安装MongoDB
linux·运维·mongodb
武子康1 天前
Java-163 MongoDB 生产安全加固实战:10 分钟完成认证、最小权限、角色详解
java·数据库·分布式·mongodb·性能优化·系统架构·nosql
金仓拾光集1 天前
国产化转型实战:制造业供应链物流系统从MongoDB至金仓数据库迁移全指南
数据库·mongodb·数据库平替用金仓·金仓数据库
金仓拾光集1 天前
金仓替代MongoDB:互联网医院聊天脱敏实战
数据库·mongodb·kingbase·kingbasees·数据库平替用金仓·金仓数据库
武子康2 天前
Java-164 MongoDB 认证与权限实战:单实例与分片集群 整体认证配置实战 最小化授权/错误速查/回滚剧本
java·数据库·分布式·mongodb·性能优化·系统架构·nosql
金仓拾光集2 天前
《MongoDB 重建索引要锁库?金仓却能边跑边修》
数据库·mongodb·kingbase·kingbasees·数据库平替用金仓·金仓数据库
金仓拾光集2 天前
金仓数据库平替MongoDB:医共体数据互通的高效安全之道
数据库·安全·mongodb·kingbase·kingbasees·数据库平替用金仓·金仓数据库
金仓拾光集2 天前
金仓替代MongoDB:安全与性能协同提升——社交用户画像系统的国产化实践
数据库·安全·mongodb·kingbase·kingbasees·数据库平替用金仓·金仓数据库
FinTech老王2 天前
国产数据库替换MongoDB实战:浙江人民医院电子病历系统国产化升级案例
数据库·mongodb