mongodb spring distinct -凯发k8网页登录

mongodb spring distinct

spring 框架下 如果要做去重,在数据量大的时候会爆error,可改用如下 写法:

    private boolean needreordercheck(string requestid) {
        boolean result = false;
//        try(mongocursor mongocursor = 
//                mongotemplate.getcollection(mongotemplate.getcollectionname(accountnumproductlineindex.class))
//                             .distinct(key, filters.eq(request_id, requestid), string.class)
//                             .iterator()
//                )
        try(mongocursor 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 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 阅读(110) 评论(0)  编辑  收藏 所属分类: springmongodbspring boot


只有注册用户后才能发表评论。


网站导航:
              
 
网站地图