Java Code Examples for com.mongodb.DBCursor#skip()

The following examples show how to use com.mongodb.DBCursor#skip() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrivateStorageRepository.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 2
Source File: SelectVo.java    From tangyuan2 with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 3
Source File: DataServlet.java    From BLELocalization with MIT License 4 votes vote down vote up
/**
 * @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);
}