目录
- 一、说明
- 二、代码案例实现
一、说明
最近项目接到一个功能点,需要对状态值status字段按照规则排序。这个status在表存储的是String纯字母,另外排序要求又不能按照字典排序方法。那这种问题如何解决?
MongoDB暂时只支持按照某些字段的升序或者降序排列。但是,在某些特别场景下, 比如对中文有要求按照指定规则排序,此时就是MongoDB的自定义排序规则。文章来源:https://uudwc.com/A/GdaMr
二、代码案例实现
通过集合方式来自定义查询文章来源地址https://uudwc.com/A/GdaMr
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();