Java Code Examples for com.mongodb.client.FindIterable#forEach()

The following examples show how to use com.mongodb.client.FindIterable#forEach() . 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: MongoSplitHandler.java    From DBus with Apache License 2.0 6 votes vote down vote up
/**
 * 查询每个shard 的主库地址
 */
private Map<String, String> getShardedUrls(MongoManager mongoManager, String username, String password) {
    FindIterable<Document> documents = mongoManager.find("config", "shards");
    Map<String, String> shardsUrlMap = new HashMap<>();
    documents.forEach(new Block<Document>() {
        @Override
        public void apply(Document document) {
            //格式:shard1/localhost:27017,localhost:27018,localhost:27019
            String[] shardHosts = document.getString("host").split("/");
            String shardName = shardHosts[0];
            String url = shardHosts[1];

            shardsUrlMap.put(shardName, url);
        }
    });
    return shardsUrlMap;
}
 
Example 2
Source File: FeatureDao.java    From XBDD with Apache License 2.0 6 votes vote down vote up
public List<XbddFeature> getFeatures(final Coordinates coordinates) {
	final MongoCollection<XbddFeature> features = getFeatureCollection();

	final List<XbddFeature> extractedFeatures = new ArrayList<>();

	final Bson query = Filters.eq("coordinates", CoordinatesMapper.mapCoordinates(coordinates));
	final FindIterable<XbddFeature> savedFeatures = features.find(query, XbddFeature.class);

	final Consumer<XbddFeature> addToExtractedFeatures = feature -> {
		if (featureIsValid(feature)) {
			extractedFeatures.add(feature);
		}
	};
	savedFeatures.forEach(addToExtractedFeatures);

	return extractedFeatures;
}
 
Example 3
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void queryAll() {
	// @begin: query-all
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants").find();
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document.toJson());
		}
	});
	// @code: end
	// @end: query-all
}
 
Example 4
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void logicalAnd() {

	// @begin: logical-and
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants")
		.find(new Document("cuisine", "Italian").append("address.zipcode", "10075"));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(and(eq("cuisine", "Italian"), eq("address.zipcode", "10075")));
	// @code: end

	// @end: logical-and
}
 
Example 5
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void logicalOr() {

	// @begin: logical-or
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants").find(new Document("$or",
		asList(new Document("cuisine", "Italian"), new Document("address.zipcode", "10075"))));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(or(eq("cuisine", "Italian"), eq("address.zipcode", "10075")));
	// @code: end

	// @end: logical-or
}
 
Example 6
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void queryTopLevelField() {
	// @begin: query-top-level-field
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants").find(new Document("borough", "Manhattan"));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(eq("borough", "Manhattan"));
	// @code: end
	// @end: query-top-level-field
}
 
Example 7
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void queryEmbeddedDocument() {
	// @begin: query-embedded-document
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants")
		.find(new Document("address.zipcode", "10075"));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(eq("address.zipcode", "10075"));
	// @code: end
	// @end: query-embedded-document
}
 
Example 8
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void queryFieldInArray() {
	// @begin: query-field-in-array
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants").find(new Document("grades.grade", "B"));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(eq("grades.grade", "B"));
	// @code: end
	// @end: query-field-in-array
}
 
Example 9
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void greaterThan() {
	// @begin: greater-than
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants")
		.find(new Document("grades.score", new Document("$gt", 30)));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(gt("grades.score", 30));
	// @code: end
	// @end: greater-than
}
 
Example 10
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void lessThan() {
	// @begin: less-than
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants")
		.find(new Document("grades.score", new Document("$lt", 10)));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(lt("grades.score", 10));
	// @code: end
	// @end: less-than
}
 
Example 11
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void sort() {
	// @begin: sort
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants").find()
		.sort(new Document("borough", 1).append("address.zipcode", 1));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify sorting fields the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find().sort(ascending("borough", "address.zipcode"));
	// @code: end
	// @end: sort
}
 
Example 12
Source File: FeatureDao.java    From XBDD with Apache License 2.0 6 votes vote down vote up
public List<XbddFeatureSummary> getFeatureSummaries(final Coordinates coordinates) {
	final MongoCollection<XbddFeature> features = getFeatureCollection();
	final List<XbddFeatureSummary> summaries = new ArrayList<>();

	final Bson query = Filters.eq("coordinates", CoordinatesMapper.mapCoordinates(coordinates));
	final FindIterable<XbddFeatureSummary> savedFeatures = features.find(query, XbddFeatureSummary.class);

	final Consumer<XbddFeatureSummary> addToSummaries = feature -> {
		if (featureIsValid(feature)) {
			summaries.add(feature);
		}
	};

	savedFeatures.forEach(addToSummaries);

	return summaries;
}
 
Example 13
Source File: MongoDBTest.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void logicalAnd() {

		// @begin: logical-and
		// @code: start
		// FindIterable<Document> iterable = db.getCollection("box_vgg_fea").find(new
		// Document("domain", "shopbop")).limit(10);
		FindIterable<Document> iterable = db.getCollection("box_vgg_fea").find().limit(10);
		// @code: end

		// @pre: Iterate the results and apply a block to each resulting document.
		// @code: start
		iterable.forEach(new Block<Document>() {
			@Override
			public void apply(final Document document) {
				log.info(document.toJson());
			}
		});
		// @code: end

		// @pre: To simplify building queries the Java driver provides static helpers
		// @code: start
		// db.getCollection("restaurants").find(and(eq("cuisine", "Italian"),
		// eq("address.zipcode",
		// "10075")));
		// @code: end

		// @end: logical-and
	}
 
Example 14
Source File: MongoHelper.java    From MongoExplorer with MIT License 5 votes vote down vote up
public static List<MongoDocument> getPageOfDocuments(String collection, String queryText, int start, int take) {
	MongoCollection coll = Database.getCollection(collection);
	FindIterable main = (queryText == null) ? coll.find() : coll.find(Document.parse(queryText));
	FindIterable items = main.skip(start).limit(take);
	final ArrayList<MongoDocument> results = new ArrayList<>();

	items.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			results.add(new MongoDocument(document.toJson()));
		}
	});

	return results;
}
 
Example 15
Source File: QueryPerformTest.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Test
public void test03_queryTop() {
	FindIterable<Document> iterable = db.getCollection("tmp_collection").find().limit(5);
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			log.debug(document.toJson());
		}
	});
	Assert.assertNotNull(iterable);
}
 
Example 16
Source File: QueryPerformTest.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Test
public void test01_queryAll() {
	FindIterable<Document> iterable = db.getCollection("tmp_collection").find();
	log.debug("tmp_collection总文档数: {}", db.getCollection("tmp_collection").count());
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(Document document) {
			log.debug(document.getString("itemid"));
		}
	});
	Assert.assertNotNull(iterable);
}
 
Example 17
Source File: MongoCollectionFind.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	cfData	query	= getNamedParam(argStruct, "query", null );
	if ( query == null )
		throwException(_session, "please specify query");
	
	int	size			= getNamedIntParam(argStruct, "size", -1 );
	int	skip			= getNamedIntParam(argStruct, "skip", -1 );
	cfData sort		= getNamedParam(argStruct, 		"sort", null );
	cfData fields	= getNamedParam(argStruct, 		"fields", null );
	
	try{
		MongoCollection<Document> col = db.getCollection(collection);

		// Get the initial cursor
		FindIterable<Document>	cursor;
		long start = System.currentTimeMillis();
		Document qry = getDocument(query);
		
		cursor = col.find( qry );
		
		if ( fields != null )
			cursor = cursor.projection( getDocument(fields) );

		// Are we sorting?
		if ( sort != null )
			cursor	= cursor.sort( getDocument(sort) );
		
		// Are we limiting
		if ( skip != -1 )
			cursor	= cursor.skip(skip);
		
		// How many we bringing back
		if ( size != -1 )
			cursor = cursor.limit(size);

		// Now we can run the query
		cfArrayData	results	= cfArrayData.createArray(1);
		
		cursor.forEach( new Block<Document>() {

			@SuppressWarnings( "rawtypes" )
			@Override
			public void apply( final Document st ) {
				try {
					results.addElement( tagUtils.convertToCfData( (Map)st ) );
				} catch ( cfmRunTimeException e ) {}
			}
		} );
		
		_session.getDebugRecorder().execMongo(col, "find", qry, System.currentTimeMillis()-start);
		
		return results;
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}