下面列出了com.mongodb.DBCursor#skip ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void toPrivateStorageList(String collectionName,
List<PrivateStorage> privatesStorage,
DBCursor cursor,
int pageNumber,
int pageSize) throws JsonProcessingException {
try {
cursor.skip(pageNumber);
cursor.limit(pageSize);
while (cursor.hasNext()) {
cursor.next();
String content = jsonParsingService.toJsonString(cursor.curr().toMap());
privatesStorage.add(PrivateStorage.builder()
.collectionName(collectionName)
.collectionContent(content)
.build());
}
} finally {
cursor.close();
}
}
public DBCursor selectSet(DBCollection collection) {
DBObject fields = getFields();
DBObject query = getQuery();
DBObject orderByObject = getOrderByObject();
DBCursor cursor = null;
// 日志
log(fields, query, orderByObject);
if (null != query && null == fields) {
cursor = collection.find(query);
} else if (null == query && null != fields) {
cursor = collection.find(new BasicDBObject(), fields);
} else if (null != fields && null != query) {
cursor = collection.find(query, fields);
} else {
cursor = collection.find();
}
if (null != orderByObject) {
cursor.sort(orderByObject);
}
if (null != this.limit) {
if (null == this.limit.getOffset()) {
cursor.limit(this.limit.getRowCount());
} else {
cursor.limit(this.limit.getRowCount());
cursor.skip(this.limit.getOffset());
}
}
return cursor;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*
* Get resource
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (mDS != MongoService.getInstance()) {
System.out.println("MongoService db name is changed");
mDS = MongoService.getInstance();
mCollRef = mDS.getCollection("refpoints");
mCollSamp = mDS.getCollection("samplings");
mCollMap = mDS.getCollection("maps");
}
String type = request.getParameter("type");
String id = request.getParameter("id");
System.out.println("doGet: type=" + type + " id=" + id);
if ("file".equals(type)) {
if (id == null) {
// Get list of files
mDS.sendJSON(mDS.getFileList(), request, response);
return;
}
try {
mDS.sendFile(id, response);
} catch (Exception e) {
System.err.println("Send error: " + id);
}
return;
}
// Get document(s)
String distinct = request.getParameter("distinct");
String pipeline = request.getParameter("pipeline");
String query = request.getParameter("query");
String keys = request.getParameter("keys");
String format = request.getParameter("format");
DBObject queryObj = query != null ? (DBObject) JSON.parse(query) : null;
DBObject keysObj = keys != null ? (DBObject) JSON.parse(keys) : null;
Object result = null;
DBCollection collection = mDS.getCollection(type);
if (id != null) {
result = collection.findOne(new ObjectId(id), keysObj);
} else if (distinct != null) {
result = collection.distinct(distinct, queryObj);
} else if (pipeline != null) {
DBObject pipelineObj = (DBObject) JSON.parse(pipeline);
if (pipelineObj instanceof List<?>) {
result = collection.aggregate((List<DBObject>) pipelineObj).results();
} else {
result = collection.aggregate(pipelineObj).results();
}
} else {
DBCursor cursor = collection.find(queryObj, keysObj);
String sort = request.getParameter("sort");
String skip = request.getParameter("skip");
String limit = request.getParameter("limit");
String count = request.getParameter("count");
if (sort != null) {
cursor = cursor.sort((DBObject) JSON.parse(sort));
}
if (skip != null) {
cursor = cursor.skip(Integer.parseInt(skip));
}
if (limit != null) {
cursor = cursor.limit(Integer.parseInt(limit));
}
result = "true".equals(count) ? cursor.count() : cursor;
}
if (id != null && result == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Document %s does not exist", id));
return;
}
if ("csv".equals(format)) {
mDS.sendCSV(result, request, response);
return;
}
mDS.sendJSON(result, request, response);
}