I want to execute mongo commands in shell script.
I tried following way test.sh
#!/bin/sh
mongo myDbName
db.mycollection.findOne()
show collections
When I execute above script ./test.sh
Then mongo connection established but next commands not executed
How to execute other commands through sh script [test.sh] ?
Please help me
--shell flag can also be used for javascript files
Thank you
printf
! In a Linux environment, here's a better way to have only one file run the show. Say you have two files,mongoCmds.js
with multiple commands:and then the driver shell file,
runMongoCmds.sh
Instead, have just one file, runMongoCmds.sh containing
Bash's
printf
is much more robust thanecho
and allows for the\n
between commands to force them on multiple lines.Create a script file; write commands:
In
file.js
write your mongo query:If you want to handle it with one line it's an easy way.
In my case, I can conveniently use
\n
as separator for the next mongo command I want to execute then pipe them tomongo