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;
}