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);
}
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");
}
}
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));
}
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"));
}
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));
}
Aggregations