mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
MongoDB example updated
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.2</version>
|
||||
<version>2.7.1</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<maven.compiler.sourcre>11</maven.compiler.sourcre>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<mongock.version>4.3.8</mongock.version>
|
||||
<gson.version>2.9.0</gson.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -56,7 +57,7 @@
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.6</version>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package ru.otus.example.mongodbdemo.repositories;
|
||||
|
||||
import org.bson.Document;
|
||||
import ru.otus.example.mongodbdemo.model.Knowledge;
|
||||
|
||||
import java.util.List;
|
||||
@@ -10,5 +11,5 @@ public interface StudentRepositoryCustom {
|
||||
long getExperienceArrayLengthByStudentId(String id);
|
||||
void removeExperienceArrayElementsById(String id);
|
||||
|
||||
void printGetStudentExperienceByIdAggregationResultForStage(String studentId, int stage);
|
||||
Document getStudentExperienceByIdAggregationRawResultForStage(String studentId, int stage);
|
||||
}
|
||||
|
||||
+3
-56
@@ -13,8 +13,6 @@ import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
import ru.otus.example.mongodbdemo.model.Knowledge;
|
||||
import ru.otus.example.mongodbdemo.model.Student;
|
||||
import ru.otus.example.mongodbdemo.utils.RawResultPrinter;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -31,7 +29,6 @@ public class StudentRepositoryCustomImpl implements StudentRepositoryCustom {
|
||||
}
|
||||
|
||||
private final MongoTemplate mongoTemplate;
|
||||
private final RawResultPrinter rawResultPrinter;
|
||||
|
||||
@Override
|
||||
public List<Knowledge> getStudentExperienceById(String studentId) {
|
||||
@@ -47,7 +44,6 @@ public class StudentRepositoryCustomImpl implements StudentRepositoryCustom {
|
||||
);
|
||||
|
||||
Document rawResults = mongoTemplate.aggregate(aggregation, Student.class, Knowledge.class).getRawResults();
|
||||
rawResultPrinter.prettyPrintRawResult(rawResults);
|
||||
return mongoTemplate.aggregate(aggregation, Student.class, Knowledge.class).getMappedResults();
|
||||
}
|
||||
|
||||
@@ -70,9 +66,7 @@ public class StudentRepositoryCustomImpl implements StudentRepositoryCustom {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printGetStudentExperienceByIdAggregationResultForStage(String studentId, int stage) {
|
||||
System.out.printf("Stage: %d\n\n", stage);
|
||||
|
||||
public Document getStudentExperienceByIdAggregationRawResultForStage(String studentId, int stage) {
|
||||
AggregationOperation[] operations = new AggregationOperation[]{
|
||||
unwind("experience")
|
||||
, project().andExclude("_id").and(valueOfToArray("experience")).as("experience_map")
|
||||
@@ -83,58 +77,11 @@ public class StudentRepositoryCustomImpl implements StudentRepositoryCustom {
|
||||
, project().and("experience._id").as("_id").and("experience.name").as("name")
|
||||
};
|
||||
|
||||
String[] operationsAsStr = new String[]{
|
||||
"unwind(\"experience\")"
|
||||
, "project().andExclude(\"_id\").and(valueOfToArray(\"experience\")).as(\"experience_map\")"
|
||||
, "project().and(\"experience_map\").arrayElementAt(1).as(\"experience_id_map\")"
|
||||
, "project().and(\"experience_id_map.v\").as(\"experience_id\")"
|
||||
, "lookup(\"knowledge\", \"experience_id\", \"_id\", \"experience\")"
|
||||
, "unwind(\"experience\")"
|
||||
, "project().and(\"experience._id\").as(\"_id\").and(\"experience.name\").as(\"name\")"
|
||||
};
|
||||
|
||||
Aggregation aggregation = newAggregation(match(Criteria.where("id").is(studentId)));
|
||||
System.out.println("match(Criteria.where(\"id\").is(studentId)");
|
||||
for (int i = 1; i <= stage; i++) {
|
||||
System.out.println(operationsAsStr[i - 1]);
|
||||
aggregation.getPipeline().add(operations[i - 1]);
|
||||
}
|
||||
System.out.println();
|
||||
Document rawResults = mongoTemplate.aggregate(aggregation, Student.class, Knowledge.class).getRawResults();
|
||||
rawResultPrinter.prettyPrintRawResult(rawResults);
|
||||
|
||||
return mongoTemplate.aggregate(aggregation, Student.class, Knowledge.class).getRawResults();
|
||||
}
|
||||
/*
|
||||
@Override
|
||||
public void printGetStudentExperienceByIdAggregationResultForStage(String studentId, int stage) {
|
||||
System.out.printf("Stage: %d\n\n", stage);
|
||||
|
||||
AggregationOperation[] operations = new AggregationOperation[]{
|
||||
unwind("experience")
|
||||
, project().andExclude("_id").and(valueOfToArray("experience")).as("experience_map")
|
||||
, project().and("experience_map").arrayElementAt(1).as("experience_id_map")
|
||||
, project().and("experience_id_map.v").as("experience_id")
|
||||
, lookup("knowledge", "experience_id", "_id", "experience")
|
||||
, project().and("experience._id").as("_id").and("experience.name").as("name")
|
||||
};
|
||||
|
||||
String[] operationsAsStr = new String[]{
|
||||
"unwind(\"experience\")"
|
||||
, "project().andExclude(\"_id\").and(valueOfToArray(\"experience\")).as(\"experience_map\")"
|
||||
, "project().and(\"experience_map\").arrayElementAt(1).as(\"experience_id_map\")"
|
||||
, "project().and(\"experience_id_map.v\").as(\"experience_id\")"
|
||||
, "lookup(\"knowledge\", \"experience_id\", \"_id\", \"experience\")"
|
||||
, "project().and(\"experience._id\").as(\"_id\").and(\"experience.name\").as(\"name\")"
|
||||
};
|
||||
|
||||
Aggregation aggregation = newAggregation(match(Criteria.where("id").is(studentId)));
|
||||
System.out.println("match(Criteria.where(\"id\").is(studentId)");
|
||||
for (int i = 1; i <= stage; i++) {
|
||||
System.out.println(operationsAsStr[i - 1]);
|
||||
aggregation.getPipeline().add(operations[i - 1]);
|
||||
}
|
||||
System.out.println();
|
||||
Document rawResults = mongoTemplate.aggregate(aggregation, Student.class, Knowledge.class).getRawResults();
|
||||
rawResultPrinter.prettyPrintRawResult(rawResults);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
+32
-7
@@ -1,17 +1,17 @@
|
||||
package ru.otus.example.mongodbdemo.repositories.withlisteners;
|
||||
|
||||
import lombok.val;
|
||||
import org.bson.Document;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import ru.otus.example.mongodbdemo.events.MongoStudentCascadeSaveEventsListener;
|
||||
import ru.otus.example.mongodbdemo.model.Knowledge;
|
||||
import ru.otus.example.mongodbdemo.model.Student;
|
||||
import ru.otus.example.mongodbdemo.repositories.AbstractRepositoryTest;
|
||||
import ru.otus.example.mongodbdemo.repositories.StudentRepository;
|
||||
import ru.otus.example.mongodbdemo.utils.RawResultPrinter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -24,9 +24,12 @@ class StudentRepositoryWithListenersTest extends AbstractRepositoryTest {
|
||||
@Autowired
|
||||
private StudentRepository studentRepository;
|
||||
|
||||
@Autowired
|
||||
private RawResultPrinter rawResultPrinter;
|
||||
|
||||
@DisplayName("должен корректно сохранять студента с отсутствующими в БД знаниями")
|
||||
@Test
|
||||
void shouldCorrectSaveStudentWithNewKnowledge(){
|
||||
void shouldCorrectSaveStudentWithNewKnowledge() {
|
||||
val student = new Student("Student #2", new Knowledge("Knowledge #3"));
|
||||
val actual = studentRepository.save(student);
|
||||
assertThat(actual.getId()).isNotNull();
|
||||
@@ -35,7 +38,7 @@ class StudentRepositoryWithListenersTest extends AbstractRepositoryTest {
|
||||
|
||||
@DisplayName("должен возвращать корректный список знаний студента")
|
||||
@Test
|
||||
void shouldReturnCorrectStudentKnowledgeList(){
|
||||
void shouldReturnCorrectStudentKnowledgeList() {
|
||||
val student = studentRepository.findAll().get(0);
|
||||
List<Knowledge> knowledgeList = studentRepository.getStudentExperienceById(student.getId());
|
||||
assertThat(knowledgeList).containsExactlyInAnyOrderElementsOf(student.getExperience());
|
||||
@@ -43,12 +46,34 @@ class StudentRepositoryWithListenersTest extends AbstractRepositoryTest {
|
||||
|
||||
@DisplayName("напечатать все стадии агрегации для getStudentExperienceById")
|
||||
@Test
|
||||
void shouldPrintAggregationStagesForHetStudentExperienceByIdMethod (){
|
||||
void shouldPrintAggregationStagesForHetStudentExperienceByIdMethod() {
|
||||
val student = studentRepository.findAll().get(0);
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
studentRepository.printGetStudentExperienceByIdAggregationResultForStage(student.getId(), i);
|
||||
System.out.println("\n\n");
|
||||
val rawResults = studentRepository.getStudentExperienceByIdAggregationRawResultForStage(
|
||||
student.getId(), i);
|
||||
printStageDesc(i, rawResults);
|
||||
}
|
||||
}
|
||||
|
||||
private void printStageDesc(int stageNum, Document stageRawResults) {
|
||||
String[] operationsAsStr = new String[]{
|
||||
"match(Criteria.where(\"id\").is(studentId)",
|
||||
"unwind(\"experience\")"
|
||||
, "project().andExclude(\"_id\").and(valueOfToArray(\"experience\")).as(\"experience_map\")"
|
||||
, "project().and(\"experience_map\").arrayElementAt(1).as(\"experience_id_map\")"
|
||||
, "project().and(\"experience_id_map.v\").as(\"experience_id\")"
|
||||
, "lookup(\"knowledge\", \"experience_id\", \"_id\", \"experience\")"
|
||||
, "unwind(\"experience\")"
|
||||
, "project().and(\"experience._id\").as(\"_id\").and(\"experience.name\").as(\"name\")"
|
||||
};
|
||||
|
||||
System.out.printf("Stage: %d\n\n", stageNum);
|
||||
for (int j = 0; j <= stageNum; j++) {
|
||||
System.out.println(operationsAsStr[j]);
|
||||
}
|
||||
System.out.println();
|
||||
rawResultPrinter.prettyPrintRawResult(stageRawResults);
|
||||
System.out.println("\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user