From 089bdbf76c0eb80a2679a89cc3f305d828f35720 Mon Sep 17 00:00:00 2001 From: stvort Date: Mon, 18 Sep 2023 21:06:32 +0400 Subject: [PATCH] mongo-db-example updated --- examples/mongo-db-demo-new-mongock/pom.xml | 7 +++ .../spring-shell.log | 2 + .../mongodbdemo/commands/AppCommands.java | 50 +++++++++++++++++++ .../src/main/resources/application.yml | 14 +++++- .../src/test/resources/application.yml | 9 ++++ 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 examples/mongo-db-demo-new-mongock/spring-shell.log create mode 100644 examples/mongo-db-demo-new-mongock/src/main/java/ru/otus/example/mongodbdemo/commands/AppCommands.java diff --git a/examples/mongo-db-demo-new-mongock/pom.xml b/examples/mongo-db-demo-new-mongock/pom.xml index 3731c094..3f6b62f2 100644 --- a/examples/mongo-db-demo-new-mongock/pom.xml +++ b/examples/mongo-db-demo-new-mongock/pom.xml @@ -25,6 +25,7 @@ 2.10.1 2.0 32.0.0-jre + 3.0.3 @@ -34,6 +35,12 @@ spring-boot-starter-data-mongodb + + org.springframework.shell + spring-shell-starter + ${spring.shell.version} + + org.yaml snakeyaml diff --git a/examples/mongo-db-demo-new-mongock/spring-shell.log b/examples/mongo-db-demo-new-mongock/spring-shell.log new file mode 100644 index 00000000..12a1429a --- /dev/null +++ b/examples/mongo-db-demo-new-mongock/spring-shell.log @@ -0,0 +1,2 @@ +1695056720328:m +1695056755656:exit diff --git a/examples/mongo-db-demo-new-mongock/src/main/java/ru/otus/example/mongodbdemo/commands/AppCommands.java b/examples/mongo-db-demo-new-mongock/src/main/java/ru/otus/example/mongodbdemo/commands/AppCommands.java new file mode 100644 index 00000000..ab0a3fe4 --- /dev/null +++ b/examples/mongo-db-demo-new-mongock/src/main/java/ru/otus/example/mongodbdemo/commands/AppCommands.java @@ -0,0 +1,50 @@ +package ru.otus.example.mongodbdemo.commands; + +import lombok.RequiredArgsConstructor; +import lombok.val; +import org.bson.Document; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import ru.otus.example.mongodbdemo.repositories.StudentRepository; +import ru.otus.example.mongodbdemo.utils.RawResultPrinter; + +@RequiredArgsConstructor +@ShellComponent +public class AppCommands { + + private final StudentRepository studentRepository; + private final RawResultPrinter rawResultPrinter; + + @ShellMethod(value = "ShowLookupMagic", key = "m") + public void showLookupMagic(){ + val student = studentRepository.findAll().get(0); + for (int i = 0; i <= 7; i++) { + 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"); + } + + +} diff --git a/examples/mongo-db-demo-new-mongock/src/main/resources/application.yml b/examples/mongo-db-demo-new-mongock/src/main/resources/application.yml index 08779f76..0f0050ea 100644 --- a/examples/mongo-db-demo-new-mongock/src/main/resources/application.yml +++ b/examples/mongo-db-demo-new-mongock/src/main/resources/application.yml @@ -5,9 +5,19 @@ spring: port: 27017 database: awesomeMongo + shell: + interactive: + enabled: true + noninteractive: + enabled: false + + command: + version: + enabled: false + mongock: - runner-type: "ApplicationRunner" # default - #runner-type: "InitializingBean" + #runner-type: "ApplicationRunner" # default + runner-type: "InitializingBean" change-logs-scan-package: - ru.otus.example.mongodbdemo.changelogs mongo-db: diff --git a/examples/mongo-db-demo-new-mongock/src/test/resources/application.yml b/examples/mongo-db-demo-new-mongock/src/test/resources/application.yml index b2e7b085..c22b2d9a 100644 --- a/examples/mongo-db-demo-new-mongock/src/test/resources/application.yml +++ b/examples/mongo-db-demo-new-mongock/src/test/resources/application.yml @@ -7,6 +7,15 @@ spring: #port: 27017 #database: awesomeMongo + shell: + interactive: + enabled: true + noninteractive: + enabled: false + + command: + version: + enabled: false de: flapdoodle: mongodb: