use of org.mongodb.morphia.query.MorphiaIterator in project morphia by mongodb.
the class AggregationPipelineImpl method aggregate.
@Override
public <U> Iterator<U> aggregate(final String collectionName, final Class<U> target, final AggregationOptions options, final ReadPreference readPreference) {
LOG.debug("stages = " + stages);
Cursor cursor = collection.aggregate(stages, options, readPreference);
return new MorphiaIterator<U, U>(datastore, cursor, mapper, target, collectionName, mapper.createEntityCache());
}
use of org.mongodb.morphia.query.MorphiaIterator in project morphia by mongodb.
the class ZipCodeDataSetTest method smallestAndLargestCities.
@Test
public void smallestAndLargestCities() throws InterruptedException, TimeoutException, IOException {
Assume.assumeTrue(new File(MONGO_IMPORT).exists());
installSampleData();
getMorphia().mapPackage(getClass().getPackage().getName());
AggregationPipeline pipeline = getDs().createAggregation(City.class).group(id(grouping("state"), grouping("city")), grouping("pop", sum("pop"))).sort(ascending("pop")).group("_id.state", grouping("biggestCity", last("_id.city")), grouping("biggestPop", last("pop")), grouping("smallestCity", first("_id.city")), grouping("smallestPop", first("pop"))).project(projection("_id").suppress(), projection("state", "_id"), projection("biggestCity", projection("name", "biggestCity"), projection("pop", "biggestPop")), projection("smallestCity", projection("name", "smallestCity"), projection("pop", "smallestPop")));
Iterator<State> iterator = pipeline.aggregate(State.class);
try {
Map<String, State> states = new HashMap<String, State>();
while (iterator.hasNext()) {
State state = iterator.next();
states.put(state.getState(), state);
}
State state = states.get("SD");
Assert.assertEquals("SIOUX FALLS", state.getBiggest().getName());
Assert.assertEquals(102046, state.getBiggest().getPopulation().longValue());
Assert.assertEquals("ZEONA", state.getSmallest().getName());
Assert.assertEquals(8, state.getSmallest().getPopulation().longValue());
} finally {
((MorphiaIterator) iterator).close();
}
}
use of org.mongodb.morphia.query.MorphiaIterator in project morphia by mongodb.
the class ZipCodeDataSetTest method validate.
private void validate(final Iterator<Population> iterator, final String state, final long value) {
boolean found = false;
try {
while (iterator.hasNext()) {
Population population = iterator.next();
if (population.getState().equals(state)) {
found = true;
Assert.assertEquals(new Long(value), population.getPopulation());
}
LOG.debug("population = " + population);
}
Assert.assertTrue("Should have found " + state, found);
} finally {
((MorphiaIterator) iterator).close();
}
}
Aggregations