我想在shell脚本中执行mongo
个命令,例如在脚本test.sh
中:
#!/bin/sh
mongo myDbName
db.mycollection.findOne()
show collections
当我通过./test.sh
执行这个脚本时,就建立了与MongoDB的连接,但没有执行以下命令.
How to execute other commands through shell script 100?
我想在shell脚本中执行mongo
个命令,例如在脚本test.sh
中:
#!/bin/sh
mongo myDbName
db.mycollection.findOne()
show collections
当我通过./test.sh
执行这个脚本时,就建立了与MongoDB的连接,但没有执行以下命令.
How to execute other commands through shell script 100?
如果某个命令只是一个命令,也可以使用--eval
标志对其求值.
mongo --eval "printjson(db.serverStatus())"
Please note:如果您使用的是Mongo运算符,以$sign开头,那么您需要将eval参数用单引号括起来,以防止shell将运算符作为环境变量进行求值:
mongo --eval 'db.mycollection.update({"name":"foo"},{$set:{"this":"that"}});' myDbName
否则你可能会看到这样的情况:
mongo --eval "db.test.update({\"name\":\"foo\"},{$set:{\"this\":\"that\"}});"
> E QUERY SyntaxError: Unexpected token :