Search in sources :

Example 11 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class AggregationTests method shouldUnwindPreserveEmpty.

// DATAMONGO-1391
@Test
@MongoVersion(asOf = "3.2")
public void shouldUnwindPreserveEmpty() {
    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", true), // 
    sort(DESC, "n"));
    AggregationResults<Document> results = mongoTemplate.aggregate(agg, INPUT_COLLECTION, Document.class);
    assertThat(results, is(notNullValue()));
    List<Document> tagCount = results.getMappedResults();
    assertThat(tagCount, is(notNullValue()));
    assertThat(tagCount.size(), is(4));
    assertThat(tagCount.get(0), isBsonObject().containing("n", 2L));
    assertThat(tagCount.get(3), isBsonObject().notContaining("n"));
}
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 12 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class AggregationTests method returnFiveMostCommonLikesShouldReturnStageExecutionInformationWithExplainOptionEnabled.

// DATAMONGO-960
@Test
@MongoVersion(asOf = "2.6")
public void returnFiveMostCommonLikesShouldReturnStageExecutionInformationWithExplainOptionEnabled() {
    createUserWithLikesDocuments();
    TypedAggregation<UserWithLikes> agg = // 
    createUsersWithCommonLikesAggregation().withOptions(newAggregationOptions().explain(true).build());
    AggregationResults<LikeStats> result = mongoTemplate.aggregate(agg, LikeStats.class);
    assertThat(result.getMappedResults(), is(empty()));
    Document rawResult = result.getRawResults();
    assertThat(rawResult, is(notNullValue()));
    assertThat(rawResult.containsKey("stages"), is(true));
}
Also used : Document(org.bson.Document) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 13 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method updateMultiShouldAddValuesCorrectlyWhenUsingPushEachWithComplexTypes.

// DATAMONGO-812
@Test
@MongoVersion(asOf = "2.4")
public void updateMultiShouldAddValuesCorrectlyWhenUsingPushEachWithComplexTypes() {
    DocumentWithCollection document = new DocumentWithCollection(Collections.<Model>emptyList());
    template.save(document);
    Query query = query(where("id").is(document.id));
    assumeThat(template.findOne(query, DocumentWithCollection.class).models, hasSize(1));
    Update update = new Update().push("models").each(new ModelA("model-b"), new ModelA("model-c"));
    template.updateMulti(query, update, DocumentWithCollection.class);
    assertThat(template.findOne(query, DocumentWithCollection.class).models, hasSize(3));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 14 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class AggregationTests method graphLookupShouldBeAppliedCorrectly.

// DATAMONGO-1551
@Test
@MongoVersion(asOf = "3.4")
public void graphLookupShouldBeAppliedCorrectly() {
    Employee em1 = Employee.builder().id(1).name("Dev").build();
    Employee em2 = Employee.builder().id(2).name("Eliot").reportsTo("Dev").build();
    Employee em4 = Employee.builder().id(4).name("Andrew").reportsTo("Eliot").build();
    mongoTemplate.insert(Arrays.asList(em1, em2, em4), Employee.class);
    TypedAggregation<Employee> agg = Aggregation.newAggregation(Employee.class, // 
    match(Criteria.where("name").is("Andrew")), // 
    Aggregation.graphLookup("employee").startWith(// 
    "reportsTo").connectFrom(// 
    "reportsTo").connectTo(// 
    "name").depthField(// 
    "depth").maxDepth(// 
    5).as("reportingHierarchy"));
    AggregationResults<Document> result = mongoTemplate.aggregate(agg, Document.class);
    Document object = result.getUniqueMappedResult();
    List<Object> list = (List<Object>) object.get("reportingHierarchy");
    assertThat(object, isBsonObject().containing("reportingHierarchy", List.class));
    assertThat((Document) list.get(0), isBsonObject().containing("name", "Dev").containing("depth", 1L));
    assertThat((Document) list.get(1), isBsonObject().containing("name", "Eliot").containing("depth", 0L));
}
Also used : IsBsonObject(org.springframework.data.mongodb.test.util.IsBsonObject) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 15 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class AggregationTests method shouldGroupByAndLookupPeopleCorectly.

// DATAMONGO-1326
@Test
@MongoVersion(asOf = "3.2")
public void shouldGroupByAndLookupPeopleCorectly() {
    createUsersWithReferencedPersons();
    TypedAggregation<User> agg = newAggregation(// 
    User.class, // 
    group().min("id").as("foreignKey"), // 
    lookup("person", "foreignKey", "firstname", "linkedPerson"), sort(ASC, "foreignKey", "linkedPerson.firstname"));
    AggregationResults<Document> results = mongoTemplate.aggregate(agg, User.class, Document.class);
    List<Document> mappedResults = results.getMappedResults();
    Document firstItem = mappedResults.get(0);
    assertThat(firstItem, isBsonObject().containing("foreignKey", "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