国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【mongodb】--自定義排序規(guī)則

這篇具有很好參考價(jià)值的文章主要介紹了【mongodb】--自定義排序規(guī)則。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、說明

最近項(xiàng)目接到一個(gè)功能點(diǎn),需要對(duì)狀態(tài)值status字段按照規(guī)則排序。這個(gè)status在表存儲(chǔ)的是String純字母,另外排序要求又不能按照字典排序方法。那這種問題如何解決?
MongoDB暫時(shí)只支持按照某些字段的升序或者降序排列。但是,在某些特別場(chǎng)景下, 比如對(duì)中文有要求按照指定規(guī)則排序,此時(shí)就是MongoDB的自定義排序規(guī)則。

二、代碼案例實(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • elasticsearch[七]:ES評(píng)分規(guī)則詳解[查詢?cè)u(píng)分規(guī)則、自定義評(píng)分規(guī)則]

    elasticsearch[七]:ES評(píng)分規(guī)則詳解[查詢?cè)u(píng)分規(guī)則、自定義評(píng)分規(guī)則]

    因?yàn)樾枰獙?duì)搜索結(jié)果進(jìn)行一個(gè)統(tǒng)一化的評(píng)分,因此需要仔細(xì)研究 ES 本身的評(píng)分規(guī)則從而想辦法把評(píng)分統(tǒng)一。 省流:無(wú)法確切統(tǒng)一化 之前有說過 ES 的查詢?cè)u(píng)分原理,那么仔細(xì)思考之后就會(huì)發(fā)現(xiàn),長(zhǎng)文本搜索對(duì)應(yīng)的 score 會(huì)比短文本搜索的 score 高很多:score = 單個(gè)分詞評(píng)分之和

    2024年01月20日
    瀏覽(26)
  • List按指定規(guī)則排序的四種方法

    使用Collections.sort(list)可對(duì)一個(gè)List對(duì)象進(jìn)行升序排序,但如果要按某種指定規(guī)則進(jìn)行排序,可使用如下四種方法: 1. 使用list.sort(comparator)方法 List的sort()方法中可以傳入一個(gè)自定義Comparator比較器。實(shí)現(xiàn)Comparator接口, 重寫compare方法 來(lái)定義排序規(guī)則。 如果compare()方法返回負(fù)整

    2024年02月05日
    瀏覽(18)
  • Mysql 創(chuàng)建數(shù)據(jù)庫(kù)字符集與排序規(guī)則

    新版本數(shù)據(jù)庫(kù)默認(rèn)編碼格式是? utf8mb4 , utf8mb4 ?比? utf8 ?多了? emoji ?編碼支持,建議普通表使用? utf8 ?如果這個(gè)表需要支持? emoji ?就使用? utf8mb4 ,也可以全部用? utf8mb4 , utf8mb4 完全向下兼容 utf8 。 字符集 當(dāng)數(shù)據(jù)庫(kù)需要適應(yīng)不同的語(yǔ)言就需要有不同的字符集,如果不指

    2024年02月07日
    瀏覽(21)
  • 工作中mongoDB排序內(nèi)容超出sort默認(rèn)內(nèi)存

    org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 96 and error message ‘Executor error during find command :: caused by :: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.’ on server mongodb.miniserver.com:27017; nested exception is com.mongodb.MongoQuery

    2024年02月10日
    瀏覽(18)
  • 【c語(yǔ)言】對(duì)結(jié)構(gòu)體數(shù)組按照某項(xiàng)規(guī)則進(jìn)行排序

    【c語(yǔ)言】對(duì)結(jié)構(gòu)體數(shù)組按照某項(xiàng)規(guī)則進(jìn)行排序

    ? ? ? ? 這是基于qsort()函數(shù)進(jìn)行的簡(jiǎn)單排序。(附帶其他類型的數(shù)組使用qsort()進(jìn)行的排序) 目錄 一、qsort()函數(shù) 二、compare()函數(shù) 1.結(jié)構(gòu)體數(shù)組 1)升序?qū)崿F(xiàn) 2)降序?qū)崿F(xiàn) 2.整型數(shù)組 為什么不直接返回?ab(a )? 如果就是想用?ab(a )返回? ?1)升序?qū)崿F(xiàn) 2)降序?qū)崿F(xiàn) 3.浮點(diǎn)

    2024年02月03日
    瀏覽(18)
  • docker鏡像版本號(hào)規(guī)則定義

    major version.minor version.patch version 是一種常用的版本號(hào)命名規(guī)則,也被稱為語(yǔ)義化版本號(hào)(Semantic Versioning)。其中: major version :表示主要版本號(hào),當(dāng)軟件發(fā)生不兼容的變化時(shí)需要更新此版本號(hào); minor version :表示次要版本號(hào),當(dāng)軟件增加新功能但仍然向下兼容時(shí)需要更新此版

    2024年01月17日
    瀏覽(22)
  • 深入了解 Python MongoDB 操作:排序、刪除、更新、結(jié)果限制全面解析

    使用 sort() 方法對(duì)結(jié)果進(jìn)行升序或降序排序。 sort() 方法接受一個(gè)參數(shù)用于“字段名”,一個(gè)參數(shù)用于“方向”(升序是默認(rèn)方向)。 示例 按名稱按字母順序?qū)Y(jié)果進(jìn)行排序: 要?jiǎng)h除一個(gè)文檔,我們使用 delete_one() 方法。 delete_one() 方法的第一個(gè)參數(shù)是一個(gè)查詢對(duì)象,用于定

    2024年01月15日
    瀏覽(25)
  • 如何使用自定義的CSS規(guī)則來(lái)定義特定類型的元素樣式

    當(dāng)我們需要對(duì)某些特定類型的元素進(jìn)行樣式定義時(shí),可以使用自定義的CSS規(guī)則來(lái)實(shí)現(xiàn)。下面我們將從新手的角度,用幽默的語(yǔ)氣來(lái)解釋如何使用自定義CSS規(guī)則來(lái)定義特定類型的元素樣式。 首先,讓我們來(lái)看看如何使用自定義CSS規(guī)則來(lái)定義特定類型的元素樣式。假設(shè)我們有一個(gè)

    2024年02月05日
    瀏覽(17)
  • MySQL 知識(shí)點(diǎn)分享一:utf8 字符集和排序規(guī)則

    我們經(jīng)常能在數(shù)據(jù)庫(kù)中看到這些: utf8mb4 和 utf8, utf8mb4_unicode_ci, utf8mb4_general_ci, utf8mb4_bin 分別代表什么意思呢? 其實(shí)他們表示的是字符集 和 排序規(guī)則 字符集:就是用來(lái)定義字符在數(shù)據(jù)庫(kù)中的編碼的集合。 排序規(guī)則:用來(lái)定義比較字符串的方式。 字符集和排序規(guī)則是一對(duì)多的關(guān)

    2024年02月07日
    瀏覽(29)
  • 使用ETLCloud強(qiáng)大的自定義規(guī)則實(shí)現(xiàn)自定義數(shù)據(jù)處理算法

    使用ETLCloud強(qiáng)大的自定義規(guī)則實(shí)現(xiàn)自定義數(shù)據(jù)處理算法

    實(shí)時(shí)數(shù)據(jù)處理規(guī)則有什么作用 ? 在大數(shù)據(jù)中的實(shí)時(shí)數(shù)據(jù)采集、ETL批量數(shù)據(jù)傳輸過程中很多數(shù)據(jù)處理過程以及數(shù)據(jù)質(zhì)量都希望實(shí)時(shí)進(jìn)行處理和檢測(cè)并把不符合要求的臟數(shù)據(jù)過濾掉或者進(jìn)行實(shí)時(shí)的數(shù)據(jù)質(zhì)量告警等。 在數(shù)據(jù)倉(cāng)庫(kù)建設(shè)過程中,每家企業(yè)的數(shù)據(jù)處理過程中肯定會(huì)有一

    2024年02月08日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包