Search in sources :

Example 46 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class ProcurementMethodSearchController method procurementMethods.

@ApiOperation(value = "Display the available procurement methods. " + "These are taken from tender.procurementMethod")
@RequestMapping(value = "/api/ocds/procurementMethod/all", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> procurementMethods() {
    DBObject project = new BasicDBObject(MongoConstants.FieldNames.TENDER_PROC_METHOD, 1);
    Aggregation agg = newAggregation(new CustomOperation(new BasicDBObject("$project", project)), group(ref(MongoConstants.FieldNames.TENDER_PROC_METHOD)));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 47 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project spring-data-mongodb by spring-projects.

the class ChangeStreamTask method prepareFilter.

List<Document> prepareFilter(MongoTemplate template, ChangeStreamOptions options) {
    if (!options.getFilter().isPresent()) {
        return Collections.emptyList();
    }
    Object filter = options.getFilter().get();
    if (filter instanceof Aggregation) {
        Aggregation agg = (Aggregation) filter;
        AggregationOperationContext context = agg instanceof TypedAggregation ? new TypeBasedAggregationOperationContext(((TypedAggregation<?>) agg).getInputType(), template.getConverter().getMappingContext(), queryMapper) : Aggregation.DEFAULT_CONTEXT;
        return agg.toPipeline(new PrefixingDelegatingAggregationOperationContext(context, "fullDocument", blacklist));
    } else if (filter instanceof List) {
        return (List<Document>) filter;
    } else {
        throw new IllegalArgumentException("ChangeStreamRequestOptions.filter mut be either an Aggregation or a plain list of Documents");
    }
}
Also used : TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) List(java.util.List) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonDocument(org.bson.BsonDocument) FullDocument(com.mongodb.client.model.changestream.FullDocument) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext)

Example 48 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project spring-data-mongodb by spring-projects.

the class AggregationTests method shouldAggregateEmptyCollectionAndStream.

// DATAMONGO-1637
@Test
public void shouldAggregateEmptyCollectionAndStream() {
    Aggregation aggregation = newAggregation(// 
    project("tags"), // 
    unwind("tags"), // 
    group("tags").count().as(// 
    "n"), // 
    project("n").and("tag").previousOperation(), // 
    sort(DESC, "n"));
    CloseableIterator<TagCount> results = mongoTemplate.aggregateStream(aggregation, INPUT_COLLECTION, TagCount.class);
    assertThat(results, is(notNullValue()));
    List<TagCount> tagCount = toList(results);
    results.close();
    assertThat(tagCount.size(), is(0));
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) Test(org.junit.Test)

Example 49 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project spring-data-mongodb by spring-projects.

the class AggregationTests method shouldAllowGroupByIdFields.

// DATAMONGO-806
@Test
public void shouldAllowGroupByIdFields() {
    mongoTemplate.dropCollection(User.class);
    LocalDateTime now = new LocalDateTime();
    User user1 = new User("u1", new PushMessage("1", "aaa", now.toDate()));
    User user2 = new User("u2", new PushMessage("2", "bbb", now.minusDays(2).toDate()));
    User user3 = new User("u3", new PushMessage("3", "ccc", now.minusDays(1).toDate()));
    mongoTemplate.save(user1);
    mongoTemplate.save(user2);
    mongoTemplate.save(user3);
    Aggregation agg = newAggregation(// 
    project("id", "msgs"), // 
    unwind("msgs"), // 
    match(where("msgs.createDate").gt(now.minusDays(1).toDate())), // 
    group("id").push("msgs").as("msgs"));
    AggregationResults<Document> results = mongoTemplate.aggregate(agg, User.class, Document.class);
    List<Document> mappedResults = results.getMappedResults();
    Document firstItem = mappedResults.get(0);
    assertThat(firstItem.get("_id"), is(notNullValue()));
    assertThat(String.valueOf(firstItem.get("_id")), is("u1"));
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) Document(org.bson.Document) Test(org.junit.Test)

Example 50 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project spring-data-mongodb by spring-projects.

the class AggregationTests method shouldApplyCountCorrectly.

// DATAMONGO-1549
@Test
@MongoVersion(asOf = "3.4")
public void shouldApplyCountCorrectly() {
    mongoTemplate.save(new Reservation("0123", "42", 100));
    mongoTemplate.save(new Reservation("0360", "43", 200));
    mongoTemplate.save(new Reservation("0360", "44", 300));
    Aggregation agg = newAggregation(// 
    count().as("documents"), // 
    project("documents").andExpression("documents * 2").as("twice"));
    AggregationResults<Document> result = mongoTemplate.aggregate(agg, Reservation.class, Document.class);
    assertThat(result.getMappedResults(), hasSize(1));
    Document document = result.getMappedResults().get(0);
    assertThat(document, isBsonObject().containing("documents", 3).containing("twice", 6));
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) Document(org.bson.Document) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Aggregations

Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)113 DBObject (com.mongodb.DBObject)79 ApiOperation (io.swagger.annotations.ApiOperation)79 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)79 BasicDBObject (com.mongodb.BasicDBObject)73 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)73 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)55 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)22 Test (org.junit.Test)20 CustomOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation)16 Document (org.bson.Document)14 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)5 JsonView (com.fasterxml.jackson.annotation.JsonView)4 Criteria (org.springframework.data.mongodb.core.query.Criteria)4 CustomUnwindOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation)3 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 List (java.util.List)2 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)2 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2