Search in sources :

Example 1 with MorphiaIterator

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());
}
Also used : MorphiaIterator(org.mongodb.morphia.query.MorphiaIterator) Cursor(com.mongodb.Cursor)

Example 2 with MorphiaIterator

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();
    }
}
Also used : HashMap(java.util.HashMap) State(org.mongodb.morphia.aggregation.zipcode.State) MorphiaIterator(org.mongodb.morphia.query.MorphiaIterator) File(java.io.File) Test(org.junit.Test)

Example 3 with MorphiaIterator

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();
    }
}
Also used : Population(org.mongodb.morphia.aggregation.zipcode.Population) MorphiaIterator(org.mongodb.morphia.query.MorphiaIterator)

Aggregations

MorphiaIterator (org.mongodb.morphia.query.MorphiaIterator)3 Cursor (com.mongodb.Cursor)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 Population (org.mongodb.morphia.aggregation.zipcode.Population)1 State (org.mongodb.morphia.aggregation.zipcode.State)1