我有一个 shell 脚本来执行一些 mongo db 操作:
例如mongo testdb --eval "db.dropDatabase()"
但是,如果 mongod 服务器没有运行,我会得到:
MongoDB shell version: 2.0.4
connecting to: testdb
Tue May 14 04:33:58 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
有没有办法在mongo中检查连接状态?最终我想要类似的东西:
if(mongod is running):
mongo testdb --eval "db.dropDatabase()"
else:
echo "pls make sure your mongod is running"
exit 1