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