2

For executing a single query script (query.js) from the command line the following code was enough.

    mongo db-name < query.js

I would like to execute (or match) multiple query files, such as query1.js, query2.js and so on. I tried the following code with no success.

    mongo db-name < query*

Please help me out here.

1 Answer 1

1

mongo will not allow you to do it you will get an amibgous redirect error. Do this

cat query* | mongo --nodb

the | takes the output of cat query* passes it as input to mongo which in turn executes any thing it gets. cat does not execute the queries it only outputs the content of the files and passes it to mongo.

Sign up to request clarification or add additional context in comments.

5 Comments

Could you explain the --nodb
oh, the --nodb option, i don't have any mongod running , if i had not used the --nodb option mongo will try to connect to a port that is not open which is the default mongo port. You don't need the --nodb option
if this solved your problem, kindly mark it as the correct answer
Looking into it right now, shall mark it when done. Thanks by the way.
Hey @0.sh your method works, but there is a slight thing to be noted, you can do thing using mongo alone. mongo db-name < query* works as well. I got an error previously because there was a semicolon missing in one of the query files (query1.js) and because of a formatting error, you cannot format it with Prettier or other JS extensions. Even though it is saved as a js file, it is a mongo query and hence requires to be treated as such. So do not format as a js file if you are trying to execute it as a mongo script. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.