use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.
the class AggregationTests method bucketShouldCollectDocumentsIntoABucket.
// DATAMONGO-1552
@Test
@MongoVersion(asOf = "3.4")
public void bucketShouldCollectDocumentsIntoABucket() {
Art a1 = Art.builder().id(1).title("The Pillars of Society").artist("Grosz").year(1926).price(199.99).build();
Art a2 = Art.builder().id(2).title("Melancholy III").artist("Munch").year(1902).price(280.00).build();
Art a3 = Art.builder().id(3).title("Dancer").artist("Miro").year(1925).price(76.04).build();
Art a4 = Art.builder().id(4).title("The Great Wave off Kanagawa").artist("Hokusai").price(167.30).build();
mongoTemplate.insert(Arrays.asList(a1, a2, a3, a4), Art.class);
TypedAggregation<Art> aggregation = newAggregation(//
Art.class, //
bucket("price").withBoundaries(0, 100, //
200).withDefaultBucket(//
"other").andOutputCount().as(//
"count").andOutput("title").push().as(//
"titles").andOutputExpression("price * 10").sum().as("sum"));
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, Document.class);
assertThat(result.getMappedResults().size(), is(3));
// { "_id" : 0 , "count" : 1 , "titles" : [ "Dancer"] , "sum" : 760.4000000000001}
Document bound0 = result.getMappedResults().get(0);
assertThat(bound0, isBsonObject().containing("count", 1).containing("titles.[0]", "Dancer"));
assertThat((Double) bound0.get("sum"), is(closeTo(760.40, 0.1)));
// { "_id" : 100 , "count" : 2 , "titles" : [ "The Pillars of Society" , "The Great Wave off Kanagawa"] , "sum" :
// 3672.9}
Document bound100 = result.getMappedResults().get(1);
assertThat(bound100, isBsonObject().containing("count", 2).containing("_id", 100));
assertThat((List<String>) bound100.get("titles"), hasItems("The Pillars of Society", "The Great Wave off Kanagawa"));
assertThat((Double) bound100.get("sum"), is(closeTo(3672.9, 0.1)));
}
use of org.springframework.data.mongodb.test.util.MongoVersion 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));
}
use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.
the class AggregationTests method shouldUnwindWithIndex.
// DATAMONGO-1391
@Test
@MongoVersion(asOf = "3.2")
public void shouldUnwindWithIndex() {
MongoCollection<Document> coll = mongoTemplate.getCollection(INPUT_COLLECTION);
coll.insertOne(createDocument("Doc1", "spring", "mongodb", "nosql"));
coll.insertOne(createDocument("Doc2"));
Aggregation agg = newAggregation(//
project("tags"), //
unwind("tags", "n"), //
project("n").and("tag").previousOperation(), //
sort(DESC, "n"));
AggregationResults<TagCount> results = mongoTemplate.aggregate(agg, INPUT_COLLECTION, TagCount.class);
assertThat(results, is(notNullValue()));
List<TagCount> tagCount = results.getMappedResults();
assertThat(tagCount, is(notNullValue()));
assertThat(tagCount.size(), is(3));
}
use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.
the class AggregationTests method bucketAutoShouldCollectDocumentsIntoABucket.
// DATAMONGO-1552
@Test
@MongoVersion(asOf = "3.4")
public void bucketAutoShouldCollectDocumentsIntoABucket() {
Art a1 = Art.builder().id(1).title("The Pillars of Society").artist("Grosz").year(1926).price(199.99).build();
Art a2 = Art.builder().id(2).title("Melancholy III").artist("Munch").year(1902).price(280.00).build();
Art a3 = Art.builder().id(3).title("Dancer").artist("Miro").year(1925).price(76.04).build();
Art a4 = Art.builder().id(4).title("The Great Wave off Kanagawa").artist("Hokusai").price(167.30).build();
mongoTemplate.insert(Arrays.asList(a1, a2, a3, a4), Art.class);
TypedAggregation<Art> aggregation = newAggregation(//
Art.class, //
bucketAuto(ArithmeticOperators.Multiply.valueOf("price").multiplyBy(10), 3).withGranularity(//
Granularities.E12).andOutputCount().as(//
"count").andOutput("title").push().as(//
"titles").andOutputExpression("price * 10").sum().as("sum"));
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, Document.class);
assertThat(result.getMappedResults().size(), is(3));
// { "min" : 680.0 , "max" : 820.0 , "count" : 1 , "titles" : [ "Dancer"] , "sum" : 760.4000000000001}
Document bound0 = result.getMappedResults().get(0);
assertThat(bound0, isBsonObject().containing("count", 1).containing("titles.[0]", "Dancer").containing("min", 680.0).containing("max"));
// { "min" : 820.0 , "max" : 1800.0 , "count" : 1 , "titles" : [ "The Great Wave off Kanagawa"] , "sum" : 1673.0}
Document bound1 = result.getMappedResults().get(1);
assertThat(bound1, isBsonObject().containing("count", 1).containing("min", 820.0));
assertThat((List<String>) bound1.get("titles"), hasItems("The Great Wave off Kanagawa"));
assertThat((Double) bound1.get("sum"), is(closeTo(1673.0, 0.1)));
}
use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.
the class AggregationTests method facetShouldCreateFacets.
// DATAMONGO-1552
@Test
@MongoVersion(asOf = "3.4")
public void facetShouldCreateFacets() {
Art a1 = Art.builder().id(1).title("The Pillars of Society").artist("Grosz").year(1926).price(199.99).build();
Art a2 = Art.builder().id(2).title("Melancholy III").artist("Munch").year(1902).price(280.00).build();
Art a3 = Art.builder().id(3).title("Dancer").artist("Miro").year(1925).price(76.04).build();
Art a4 = Art.builder().id(4).title("The Great Wave off Kanagawa").artist("Hokusai").price(167.30).build();
mongoTemplate.insert(Arrays.asList(a1, a2, a3, a4), Art.class);
BucketAutoOperation bucketPrice = //
bucketAuto(ArithmeticOperators.Multiply.valueOf("price").multiplyBy(10), 3).withGranularity(//
Granularities.E12).andOutputCount().as(//
"count").andOutput("title").push().as(//
"titles").andOutputExpression(//
"price * 10").sum().as("sum");
TypedAggregation<Art> aggregation = newAggregation(//
Art.class, //
project("title", "artist", "year", "price"), //
facet(bucketPrice).as("categorizeByPrice").and(bucketAuto("year", 3)).as("categorizeByYear"));
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, Document.class);
assertThat(result.getMappedResults().size(), is(1));
Document mappedResult = result.getUniqueMappedResult();
// [ { "_id" : { "min" : 680.0 , "max" : 820.0} , "count" : 1 , "titles" : [ "Dancer"] , "sum" : 760.4000000000001}
// ,
// { "_id" : { "min" : 820.0 , "max" : 1800.0} , "count" : 1 , "titles" : [ "The Great Wave off Kanagawa"] , "sum" :
// 1673.0} ,
// { "_id" : { "min" : 1800.0 , "max" : 3300.0} , "count" : 2 , "titles" : [ "The Pillars of Society" , "Melancholy
// III"] , "sum" : 4799.9}]
List<Object> categorizeByPrice = (List<Object>) mappedResult.get("categorizeByPrice");
assertThat(categorizeByPrice, hasSize(3));
// [ { "_id" : { "min" : null , "max" : 1902} , "count" : 1} ,
// { "_id" : { "min" : 1902-2018 , "max" : 1925} , "count" : 1} ,
// { "_id" : { "min" : 1925-2018 , "max" : 1926} , "count" : 2}]
List<Object> categorizeByYear = (List<Object>) mappedResult.get("categorizeByYear");
assertThat(categorizeByYear, hasSize(3));
}
Aggregations