Search in sources :

Example 16 with MongoVersion

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

Example 17 with MongoVersion

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));
}
Also used : Document(org.bson.Document) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 18 with MongoVersion

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())));
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) IsBsonObject(org.springframework.data.mongodb.test.util.IsBsonObject) Document(org.bson.Document) Person(org.springframework.data.mongodb.repository.Person) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 19 with MongoVersion

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"));
}
Also used : Document(org.bson.Document) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Aggregations

Test (org.junit.Test)19 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)19 Document (org.bson.Document)16 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)5 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)3 Query (org.springframework.data.mongodb.core.query.Query)3 Update (org.springframework.data.mongodb.core.query.Update)3 IsBsonObject (org.springframework.data.mongodb.test.util.IsBsonObject)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ExpressionVariable (org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable)1 Person (org.springframework.data.mongodb.repository.Person)1