mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
mongodb demo updated
This commit is contained in:
@@ -59,6 +59,13 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.6</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--Тестирование-->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
||||
+2
@@ -9,4 +9,6 @@ public interface StudentRepositoryCustom {
|
||||
|
||||
long getExperienceArrayLengthByStudentId(String id);
|
||||
void removeExperienceArrayElementsById(String id);
|
||||
|
||||
void printGetStudentExperienceByIdAggregationResultForStage(String studentId, int stage);
|
||||
}
|
||||
|
||||
+70
@@ -7,6 +7,7 @@ import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
||||
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
@@ -41,6 +42,7 @@ public class StudentRepositoryCustomImpl implements StudentRepositoryCustom {
|
||||
, 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")
|
||||
);
|
||||
|
||||
@@ -67,4 +69,72 @@ public class StudentRepositoryCustomImpl implements StudentRepositoryCustom {
|
||||
mongoTemplate.updateMulti(new Query(), update, Student.class);
|
||||
}
|
||||
|
||||
@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")
|
||||
, unwind("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\")"
|
||||
, "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);
|
||||
}
|
||||
/*
|
||||
@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);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
+18
-1
@@ -1,8 +1,12 @@
|
||||
package ru.otus.example.mongodbdemo.utils;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.mongodb.DBRef;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -12,7 +16,20 @@ public class RawResultPrinterImpl implements RawResultPrinter {
|
||||
@Override
|
||||
public void prettyPrintRawResult(Document document) {
|
||||
List<Document> results = (List<Document>) document.get("results");
|
||||
String resultsAsString = results.stream().map(Document::toString).collect(Collectors.joining("\n"));
|
||||
Gson gson = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.registerTypeAdapter(ObjectId.class, (JsonSerializer<ObjectId>) (src, typeOfSrc, context) ->
|
||||
new JsonPrimitive(src.toHexString()))
|
||||
.registerTypeAdapter(DBRef.class, (JsonSerializer<DBRef>) (src, typeOfSrc, context) -> {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.add("$ref", new JsonPrimitive(src.getCollectionName()));
|
||||
jsonObject.add("$id", new JsonPrimitive(src.getId().toString()));
|
||||
return jsonObject;
|
||||
})
|
||||
.create();
|
||||
|
||||
String resultsAsString = gson.toJson(results);
|
||||
//String resultsAsString = results.stream().map(Document::toString).collect(Collectors.joining("\n"));
|
||||
System.out.println(resultsAsString);
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -41,4 +41,14 @@ class StudentRepositoryWithListenersTest extends AbstractRepositoryTest {
|
||||
assertThat(knowledgeList).containsExactlyInAnyOrderElementsOf(student.getExperience());
|
||||
}
|
||||
|
||||
@DisplayName("напечатать все стадии агрегации для getStudentExperienceById")
|
||||
@Test
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ spring:
|
||||
mongodb:
|
||||
port: 0
|
||||
database: test
|
||||
#uri: mongodb://localhost
|
||||
#host: localhost
|
||||
#port: 27017
|
||||
#database: awesomeMongo
|
||||
|
||||
|
||||
Reference in New Issue
Block a user