如何从bash执行mongo命令? [英] How to execute mongo commands from bash?

查看:104
本文介绍了如何从bash执行mongo命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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?

推荐答案

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命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆