Search in sources :

Example 1 with State

use of org.mongodb.morphia.aggregation.zipcode.State 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)

Aggregations

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