paulwong

          MONGODB SPRING DISTINCT

          SPRING 框架下 如果要做去重,在數(shù)據(jù)量大的時候會爆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)  編輯  收藏 所屬分類: SPRINGMONGODB 、SPRING BOOT

          主站蜘蛛池模板: 景东| 晋城| 奈曼旗| 柘城县| 吉水县| 石家庄市| 濉溪县| 昭苏县| 密云县| 宁都县| 津市市| 静乐县| 巴彦县| 延津县| 江安县| 洱源县| 加查县| 天等县| 沁阳市| 永平县| 金阳县| 乌什县| 玉树县| 邛崃市| 惠安县| 集安市| 汉阴县| 凌云县| 靖宇县| 彰化市| 弋阳县| 河间市| 贵港市| 积石山| 醴陵市| 和田市| 公主岭市| 桂东县| 玛沁县| 屯门区| 大洼县|