一、說明
最近項(xiàng)目接到一個(gè)功能點(diǎn),需要對(duì)狀態(tài)值status字段按照規(guī)則排序。這個(gè)status在表存儲(chǔ)的是String純字母,另外排序要求又不能按照字典排序方法。那這種問題如何解決?
MongoDB暫時(shí)只支持按照某些字段的升序或者降序排列。但是,在某些特別場(chǎng)景下, 比如對(duì)中文有要求按照指定規(guī)則排序,此時(shí)就是MongoDB的自定義排序規(guī)則。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-707876.html
二、代碼案例實(shí)現(xiàn)
通過集合方式來(lái)自定義查詢文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-707876.html
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")));
}
// 設(shè)置分頁(yè)參數(shù)
operations.add(Aggregation.skip((page-1)*size));
operations.add(Aggregation.limit(size));
// 根據(jù)上面自定義的排序規(guī)則將對(duì)應(yīng)的數(shù)據(jù)轉(zhuǎn)換回來(lái)
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();
到了這里,關(guān)于【mongodb】--自定義排序規(guī)則的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!