public class MongoUtils {
/**
* 通过条件排序,分页查询
*
* @param collection mongo集合名称
* @param where 查询条件
* @param total 总数
* @param current 当前页
* @param pageSize 页面大小
* @param field 排序的字段
* @param orderby 1代表升序,-1为降序
* @return 分页数据
*/
public static List<Document> selectPageList(MongoCollection<Document> collection, BasicDBObject where, Long total, int current, int pageSize,String field,Object orderby) {
MongoCursor<Document> iteratorDoc = null;
List<Document> list = new ArrayList<>();
if (current <= total) {
iteratorDoc = collection.find(where).sort(new BasicDBObject("order", orderby)).skip((current - 1) * pageSize).limit(pageSize).iterator();
while (iteratorDoc.hasNext()) {
list.add(iteratorDoc.next());
}
if (iteratorDoc != null) {
iteratorDoc.close();
}
}
return list;
}
}
MongoDB分页工具类
最新推荐文章于 2024-08-10 07:06:18 发布