问题描述
我正在尝试从bash脚本运行以下命令:
I am trying to run this command from bash script:
mongo 192.168.10.20:27000 --eval "use admin && db.shutdownServer() && quit()"
但是我得到这个错误:
[rs.initiate() && use admin && db.shutdownServer() && quit()] doesn't exist
我如何在不使用js文件的情况下做到这一点?
how can i do it without using a js file?
推荐答案
有.特别是,use admin
之类的命令不是有效的JavaScript,仅在交互式shell会话中有效.
There are differences between interactive & scripted mongo
shell sessions. In particular, commands like use admin
are not valid JavaScript and will only work in an interactive shell session.
关闭命令行的等效工作是:
The working equivalent of your shutdown command line would be:
mongo 192.168.10.20:27000/admin --eval "db.shutdownServer()"
您可以在连接字符串中包含要使用的数据库,而无需退出脚本化的mongo
shell会话.
You can include the database to use in the connection string, and there is no need to quit from a scripted mongo
shell session.
如果您确实需要通过脚本化会话更改数据库,则可以使用 db.getSiblingDB()
db.getSiblingDB()
JavaScript函数.上面编写关闭命令的另一种方法是:
If you do need to change databases from a scripted session, there is a db.getSiblingDB()
JavaScript function. An alternative way to write the shutdown command above would be:
mongo 192.168.10.20:27000 --eval "db=db.getSiblingDB('admin');db.shutdownServer()"
这篇关于如何从bash执行mongo命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!