Example usage for org.springframework.data.mongodb.core.aggregation Aggregation group

List of usage examples for org.springframework.data.mongodb.core.aggregation Aggregation group

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.aggregation Aggregation group.

Prototype

public static GroupOperation group(Fields fields) 

Source Link

Document

Creates a new GroupOperation for the given Fields .

Usage

From source file:org.ingini.mongodb.spring.example.aggregation.TestAggregationFramework.java

/**
 * Example taken from http://docs.mongodb.org/manual/tutorial/aggregation-examples/#aggregations-using-the-zip-code-data-set
 * Command line import: mongoimport --drop -d aggregation_test_db -c zipcodes zips.json
 * <p>//from w w w  .ja  v a 2 s .co  m
 *      db.zipcodes.aggregate(  { $group : { _id : "$state", totalPop : { $sum : "$pop" } } },
 *                              { $match : {totalPop : { $gt : 10*1000*1000 } } } )
 * </p>
 */
@Test
public void shouldFindAllStatesWithPopulationOver10Millions() {
    //GIVEN
    CollectionManager.cleanAndFill(mongoTemplate.getDb(), "zips.json", ZipData.COLLECTION_NAME);
    int lowerLimit = 10 * 1000 * 1000;

    //WHEN
    AggregationResults<AggregatedStateData> aggregationResult = mongoTemplate.aggregate(
            Aggregation.newAggregation(StateData.class, Aggregation.group("state").sum("population").as("pop"),
                    Aggregation.match(Criteria.where("pop").gt(lowerLimit))),
            AggregatedStateData.class);

    //THEN
    assertThat(aggregationResult.getMappedResults()).hasSize(7);
}

From source file:io.smalldata.ohmageomh.data.repository.MongoDataPointRepositoryImpl.java

@Override
public List<LastDataPointDate> findLastDataPointDate(List<String> userIds,
        DataPointSearchCriteria searchCriteria, String dateField) {

    if (dateField == null) {
        dateField = "header.creation_date_time";
    }//  w w  w.jav  a 2 s .  co m

    Aggregation agg = Aggregation.newAggregation(
            Aggregation.match(Criteria.where("header.user_id").in(userIds).and("header.schema_id.namespace")
                    .is(searchCriteria.getSchemaNamespace()).and("header.schema_id.name")
                    .is(searchCriteria.getSchemaName()).and("header.schema_id.version.major")
                    .is(searchCriteria.getSchemaVersion().getMajor()).and("header.schema_id.version.minor")
                    .is(searchCriteria.getSchemaVersion().getMinor())),
            Aggregation.group("header.user_id").max(dateField).as("date").last("header.user_id").as("user_id"));

    //Convert the aggregation result into a List
    AggregationResults<LastDataPointDate> groupResults = mongoOperations.aggregate(agg, "dataPoint",
            LastDataPointDate.class);

    List<LastDataPointDate> result = groupResults.getMappedResults();

    return result;
}

From source file:org.ingini.mongodb.spring.example.aggregation.TestAggregationFramework.java

/**
 * Example taken from https://github.com/mongodb/mongo-ruby-driver/wiki/Aggregation-Framework-Examples
 * Command line import: mongoimport --drop --db aggregation_test_db --collection name_days name_days.json
 * <p>/*  w  w w  .  ja  va 2 s  . c om*/
 *      db.name_days.aggregate({$project : {names : 1, _id : 0}},
 *                             {$unwind : '$names'},
 *                             {$group : {_id: '$names', counter: {$sum: 1}}},
 *                             {$sort : {counter: -1}},
 *                             {$limit : 10});
 * </p>
 */
@Test
public void shouldFindThe10MostCommonNames() {
    //GIVEN
    CollectionManager.cleanAndFill(mongoTemplate.getDb(), "name_days.json", NameData.COLLECTION_NAME);
    int limit = 10;

    //WHEN
    AggregationResults<AggregatedNameData> aggregationResult = mongoTemplate.aggregate(
            Aggregation.newAggregation(NameData.class, Aggregation.project("names").andExclude("_id"),
                    Aggregation.unwind("names"), Aggregation.group("names").count().as("counter"),
                    Aggregation.sort(Sort.Direction.DESC, "counter"), Aggregation.limit(limit)),
            AggregatedNameData.class);

    //THEN
    assertThat(aggregationResult.getMappedResults()).hasSize(10);
    assertThat(aggregationResult.getMappedResults().get(0)).isEqualTo(new AggregatedNameData("Jana", 21));

}