0

I have a scenario where the query(aggregation one) used in mongoBash, can it be stored in a file and be executed using JAVA spring boot application. If possible, can you please share an example. Also is it possible to pass arguments to the query stored in file from JAVA?

trytocode
  • 3
  • 3

1 Answers1

0

Writing it as answer since it's quite big:

there is no tool like mongoBash, I assume you need to execute query that you took from mongo shell (legacy version is mongo or modern one which is mongosh). Then, there is no a build-in way in java driver to execute it since:

  1. Shell syntax is only supported in the shell, not in the driver.
  2. You can use db.RunCommand(..) in the java driver that allows you to provide a query in MQL (mongo query language which is completely different with the shell syntax) but it will be only one command at once.

However, you can use a shell functionality in java without the java driver via running mongo shell binary with --eval argument. To make it work in java, you will need to spawn mongo process, provide appropriate js/shell code in --eval arguments and then read the output stream. You can read my answer about this for c# here. Java code should look similar

dododo
  • 3,872
  • 1
  • 14
  • 37