先找字符串长度最长的,再按降序排列,取第一个
/**
* 查询数据 最大值(字符串型)
* fieldname 要查询的字段名
*/
public String findMaxString(String fieldname){
int i = 6;
Document document = new Document(fieldname,new Document("$regex","^.{"+i+"}$"));
FindIterable<Document> find = mcol.find(document);
MongoCursor<Document> cursor = find.iterator();
while (!cursor.hasNext()) {
i=i-1;
document = new Document(fieldname,new Document("$regex","^.{"+i+"}$"));
find = mcol.find(document);
cursor = find.iterator();
}
if (cursor.hasNext()) {
Document document1 = new Document(fieldname,-1);
FindIterable<Document> sort = find.sort(document1);
return (String) sort.first().get(fieldname);
}else{
return "0";
}
}