use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.
the class AggregationTests method shouldSupportReturningCurrentAggregationRootInReference.
/**
* {@link http://stackoverflow.com/questions/24185987/using-root-inside-spring-data-mongodb-for-retrieving-whole-document}
*/
// DATAMONGO-954
@Test
@MongoVersion(asOf = "2.6")
public void shouldSupportReturningCurrentAggregationRootInReference() {
mongoTemplate.save(new Reservation("0123", "42", 100));
mongoTemplate.save(new Reservation("0360", "43", 200));
mongoTemplate.save(new Reservation("0360", "44", 300));
Aggregation agg = newAggregation(//
match(where("hotelCode").is("0360")), //
sort(Direction.DESC, "confirmationNumber", "timestamp"), //
group("confirmationNumber").first("timestamp").as(//
"timestamp").first(Aggregation.ROOT).as(//
"reservationImage"));
AggregationResults<Document> result = mongoTemplate.aggregate(agg, Reservation.class, Document.class);
assertThat(result.getMappedResults(), hasSize(2));
}
use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.
the class AggregationTests method shouldPerformReplaceRootOperatorCorrectly.
// DATAMONGO-1550
@Test
@MongoVersion(asOf = "3.4")
public void shouldPerformReplaceRootOperatorCorrectly() throws ParseException {
Data data = new Data();
DataItem dataItem = new DataItem();
dataItem.primitiveIntValue = 42;
data.item = dataItem;
mongoTemplate.insert(data);
TypedAggregation<Data> agg = newAggregation(//
Data.class, //
project("item"), //
replaceRoot("item"), project().and("primitiveIntValue").as("my_primitiveIntValue"));
AggregationResults<Document> results = mongoTemplate.aggregate(agg, Document.class);
Document resultDocument = results.getUniqueMappedResult();
assertThat(resultDocument, is(notNullValue()));
assertThat((Integer) resultDocument.get("my_primitiveIntValue"), is(42));
assertThat((Integer) resultDocument.keySet().size(), is(1));
}
use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.
the class AggregationTests method shouldSupportReturningCurrentAggregationRoot.
// DATAMONGO-954
@Test
@MongoVersion(asOf = "2.6")
public void shouldSupportReturningCurrentAggregationRoot() {
mongoTemplate.save(new Person("p1_first", "p1_last", 25));
mongoTemplate.save(new Person("p2_first", "p2_last", 32));
mongoTemplate.save(new Person("p3_first", "p3_last", 25));
mongoTemplate.save(new Person("p4_first", "p4_last", 15));
List<Document> personsWithAge25 = mongoTemplate.find(Query.query(where("age").is(25)), Document.class, mongoTemplate.getCollectionName(Person.class));
Aggregation agg = newAggregation(group("age").push(Aggregation.ROOT).as("users"));
AggregationResults<Document> result = mongoTemplate.aggregate(agg, Person.class, Document.class);
assertThat(result.getMappedResults(), hasSize(3));
Document o = result.getMappedResults().get(2);
assertThat(o.get("_id"), is((Object) 25));
assertThat((List<?>) o.get("users"), hasSize(2));
assertThat((List<?>) o.get("users"), is(contains(personsWithAge25.toArray())));
}
use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.
the class AggregationTests method shouldLookupPeopleCorectly.
// DATAMONGO-1326
@Test
@MongoVersion(asOf = "3.2")
public void shouldLookupPeopleCorectly() {
createUsersWithReferencedPersons();
TypedAggregation<User> agg = newAggregation(//
User.class, //
lookup("person", "_id", "firstname", "linkedPerson"), sort(ASC, "id"));
AggregationResults<Document> results = mongoTemplate.aggregate(agg, User.class, Document.class);
List<Document> mappedResults = results.getMappedResults();
Document firstItem = mappedResults.get(0);
assertThat(firstItem, isBsonObject().containing("_id", "u1"));
assertThat(firstItem, isBsonObject().containing("linkedPerson.[0].firstname", "u1"));
}
Aggregations