paulwong

          MONGODB SPRING DISTINCT

          SPRING 框架下 如果要做去重,在數據量大的時候會爆ERROR,可改用如下 寫法:

              private boolean needReorderCheck(String requestId) {
                  boolean result = false;
          //        try(MongoCursor<String> mongoCursor = 
          //                mongoTemplate.getCollection(mongoTemplate.getCollectionName(AccountNumProductLineIndex.class))
          //                             .distinct(KEY, Filters.eq(REQUEST_ID, requestId), String.class)
          //                             .iterator()
          //                )
                  try(MongoCursor<Document> mongoCursor = 
                          mongoTemplate.getCollection(mongoTemplate.getCollectionName(AccountNumProductLineIndex.class))
                                       .aggregate(
                                           Arrays.asList(
                                              Aggregates.project(
                                                              Projections.fields(
                                                                              Projections.excludeId(),
                                                                             Projections.include(KEY),
                                                                             Projections.include(REQUEST_ID)
                                                                          )
                                                         ),
                                              Aggregates.match(Filters.eq(REQUEST_ID, requestId)),
                                              Aggregates.group("$" + KEY)
                                           )
                                        )
                                       .allowDiskUse(true)
                                       .iterator();
                  )
                  {
                      String key = null;
                      boolean breakMe = false;
                      LOGGER.info("needReorderCheck.key --> start");
                      while(mongoCursor.hasNext()) {
                          if(breakMe) {
                              mongoCursor.close();
                              break;
                          }
                          Document keyDocument = mongoCursor.next();
                          key = keyDocument.getString("_id");
          //                key = mongoCursor.next().getString(KEY);
          //                LOGGER.info("needReorderCheck.keyDocument --> {}, key --> {}", keyDocument, key);
                          try(MongoCursor<Document> indexMongoCursor = 
                                  mongoTemplate.getCollection(AccountNumProductLineIndex.COLLECTION_NAME)
                                                  .find(Filters.and(Filters.eq(REQUEST_ID, requestId), Filters.eq(KEY, key)))
                                                  .iterator()
                          )
                          {
                              int preIndex = -1, currentIndex = -1;
                              Document preIndexDocument = null, currentIndexDocument;
                              while(indexMongoCursor.hasNext()) {
                                  currentIndexDocument = indexMongoCursor.next();
          //                        System.out.println(currentIndexDocument.toJson());
                                  if(preIndexDocument != null) {
                                       currentIndex = currentIndexDocument.getInteger(INDEX);
                                       preIndex = preIndexDocument.getInteger(INDEX);
                                       if(currentIndex - preIndex > 1) {
                                          indexMongoCursor.close();
                                          breakMe = true;
                                          result = true;
                                          break;
                                      }
                                  }
                                  preIndexDocument = currentIndexDocument;
                              }
                          }
                      }
                  }
                  
                  return result;
              }

          posted on 2022-10-18 10:22 paulwong 閱讀(206) 評論(0)  編輯  收藏 所屬分類: SPRINGMONGODBSPRING BOOT

          主站蜘蛛池模板: 佳木斯市| 东兴市| 滨州市| 澄迈县| 宁德市| 冷水江市| 平泉县| 崇仁县| 东阿县| 焦作市| 双峰县| 金溪县| 交口县| 定兴县| 同仁县| 施秉县| 广东省| 右玉县| 贵定县| 连江县| 文安县| 龙州县| 介休市| 大竹县| 嘉兴市| 得荣县| 哈巴河县| 青岛市| 余庆县| 阿拉善右旗| 平谷区| 徐汇区| 安塞县| 环江| 彰武县| 登封市| 台前县| 巩义市| 博爱县| 如东县| 武强县|