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

The following examples show how to use com.mongodb.DBCursor#limit() . 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: AutomationStatistics.java    From XBDD with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/recent-builds/{product}")
public Response getRecentBuildStatsForProduct(@BeanParam final Coordinates coordinates, @QueryParam("limit") final Integer limit) {
	final BasicDBList returns = new BasicDBList();
	final DBCollection collection = this.mongoLegacyDb.getCollection("reportStats");
	final BasicDBObject example = coordinates.getQueryObject(Field.PRODUCT);
	final DBCursor cursor = collection.find(example).sort(Coordinates.getFeatureSortingObject());
	if (limit != null) {
		cursor.limit(limit);
	}
	try {
		while (cursor.hasNext()) {
			final DBObject doc = cursor.next();
			returns.add(doc);
		}
	} finally {
		cursor.close();
	}
	return Response.ok(SerializerUtil.serialise(returns)).build();
}
 
Example 3
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 4
Source File: AutomationStatistics.java    From XBDD with Apache License 2.0 5 votes vote down vote up
/**
 * Go through the prior versions of this product; for each version, find the latest build and contribute the stats for that to the
 * returned list.
 */
@GET
@Path("/recent-versions/{product}")
@Produces(MediaType.APPLICATION_JSON)
public Response getRecentVersionStatsForProduct(@BeanParam final Coordinates coordinates, @QueryParam("limit") final Integer limit) {
	final BasicDBList returns = new BasicDBList();

	final DBCollection summaryCollection = this.mongoLegacyDb.getCollection("summary");
	final DBCollection reportStatsCollection = this.mongoLegacyDb.getCollection("reportStats");
	final DBCursor versions = summaryCollection.find(coordinates.getQueryObject(Field.PRODUCT))
			.sort(Coordinates.getFeatureSortingObject());
	if (limit != null) {
		versions.limit(limit);
	}
	try {
		while (versions.hasNext()) { // go through each summary document
			final DBObject version = versions.next(); // each represents the coordinates for a given version
			final Coordinates c = new Coordinates((DBObject) version.get("coordinates"));
			final BasicDBList builds = (BasicDBList) version.get("builds");
			c.setBuild((String) builds.get(builds.size() - 1)); // we need to specify which build (the latest is last in the list)
			final DBObject query = c.getQueryObject();
			returns.add(reportStatsCollection.findOne(query));
		}
	} finally {
		versions.close();
	}

	return Response.ok(SerializerUtil.serialise(returns)).build();
}
 
Example 5
Source File: MongoDBQueryHelpers.java    From hvdf with Apache License 2.0 5 votes vote down vote up
public static void getSamplesFromCursor(DBCursor cursor, int limit, List<Sample> outList){
    try{
        // exhaust the cursor adding each sample
		cursor.batchSize(limit);
		cursor.limit(limit);
        while(cursor.hasNext()) {
        	outList.add(new Sample(cursor.next()));
        }
    } finally {
        // ensure cursor is closed
        cursor.close();
    }
}
 
Example 6
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);
}