I want to execute mongo
commands in shell script, e.g. in a script test.sh
:
#!/bin/sh
mongo myDbName
db.mycollection.findOne()
show collections
When I execute this script via ./test.sh
, then the connection to MongoDB is established, but the following commands are not executed.
How to execute other commands through shell script test.sh
?
Best Answer
You can also evaluate a command using the
--eval
flag, if it is just a single command.Please note: if you are using Mongo operators, starting with a $ sign, you'll want to surround the eval argument in single quotes to keep the shell from evaluating the operator as an environment variable:
Otherwise you may see something like this: