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?
Asked
Active
Viewed 66 times
0
-
What is mongoBash? – Wernfried Domscheit Dec 13 '22 at 18:13
-
Query that are run in mongoDb – trytocode Dec 14 '22 at 04:57
-
mongoBash is a query? Neuer heard about that. It seems to be rather a tool. – Wernfried Domscheit Dec 14 '22 at 05:53
-
mongoBash is not the query. Query that is run in mangoBash, can it be stored in a file and get used in a java application. I am looking for help regarding this. – trytocode Dec 14 '22 at 07:37
-
Again, I never heard about a tool named "mongoBash" - must be a brand new or very exotic tool. Since you don't provide your query either, it is difficult to help you, because nobody can know which language base is used by mongoBash. – Wernfried Domscheit Dec 14 '22 at 07:57
1 Answers
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:
- Shell syntax is only supported in the shell, not in the driver.
- 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