【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();
      
相关推荐
Wang's Blog39 分钟前
MongoDB小课堂: 游标操作与文档投影技术深度解析
数据库·mongodb
Wang's Blog1 天前
MongoDB小课堂: 高级查询操作符与游标管理综合指南之深度整合逻辑操作符、字段处理、数组查询与游标控制的最佳实践
数据库·mongodb
大侠课堂1 天前
单片机经典面试题50道
arm开发·单片机·嵌入式硬件·mongodb
垂金烟柳1 天前
MongoDB GridFS 历史数据自动化清理实践
数据库·mongodb·自动化
老刘莱国瑞1 天前
前后端开发规范 (React + Flask + MongoDB)
mongodb·react.js·flask
2501_941801762 天前
Python异步爬虫实战:从基础请求到高效数据抓取的全流程解析与优化技巧
mongodb
2501_941801762 天前
Go高性能分布式任务调度与Kubernetes实战分享:多节点任务管理与负载均衡优化经验
mongodb
U***e632 天前
ReactRESTfulAPI案例
mongodb·arcgis·语言模型
p***h6433 天前
从无到有:MongoDB事务的演进之路
数据库·mongodb
Wang's Blog3 天前
MongoDB小课堂: 容器化部署与操作综合指南
数据库·mongodb