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

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

Introduction

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

Prototype

public static <T> TypedAggregation<T> newAggregation(Class<T> type, AggregationOperation... operations) 

Source Link

Document

Creates a new TypedAggregation for the given type and AggregationOperation s.

Usage

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";
    }//from  w  w  w .j  a  v  a 2 s . c om

    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;
}