为 MongoDB shell 设置默认数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11336346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting default database for MongoDB shell
提问by Holger Sindbaek
When I go into the mongo shell in my terminal, it always starts with the database test, which is the wrong database. Can you set mongo to start in a specific database?
当我在终端中进入 mongo shell 时,它总是从数据库测试开始,这是错误的数据库。你可以设置 mongo 在特定数据库中启动吗?
回答by Stennie
Command Line
命令行
You can select the database to use on the mongo command line, eg for 'mydb':
您可以在mongo 命令行上选择要使用的数据库,例如“mydb”:
mongo mydb
If a database name is not provided, 'test' will be used.
如果未提供数据库名称,将使用“test”。
In .mongorc.js
在 .mongorc.js 中
If you want to set a default database without specifying on the command line each time, you can add a line to the .mongorc.js
file in your home directory:
如果你想设置默认数据库而不每次都在命令行指定,你可以.mongorc.js
在你的主目录中的文件中添加一行:
db = db.getSiblingDB("mydb")
The .mongorc.js
file is executed after the mongo
shell is started, so if you set a default here it will override a database specified on the command line.
该.mongorc.js
文件在mongo
shell 启动后执行,因此如果您在此处设置默认值,它将覆盖在命令行上指定的数据库。
回答by bradq
It's entirely possible to set a default, albiet in a slightly strange way. Here's what I do for using automatic authentication to an admin in .mongorc.js:
完全有可能以一种稍微奇怪的方式设置默认值。这是我在 .mongorc.js 中对管理员使用自动身份验证的操作:
//Persist the database selected
var selectedDB = db
//Authenticate
db = db.getSiblingDB("admin")
db.auth('admin','adminpass')
//Switch back to selected DB
db = selectedDB
//Universally allow read queries on secondaries from shell.
rs.slaveOk()
To directly answer the question, I'd think the way to accomplish this is simply by executing a check to see if the current database loaded up is "test" and alter only if that's the case, so.
为了直接回答这个问题,我认为实现这一点的方法只是执行检查以查看当前加载的数据库是否为“测试”,并且仅在这种情况下才进行更改。
if(db.name == 'test')
db.getSiblingDB('yourdefaultdb')
if(db.name == 'test')
db.getSiblingDB('yourdefaultdb')
This allows both selecting a database at the command line and setting a default. Naturally it'll prevent you from overriding and using the "test" db from the command line, but I think that's a bit of a strange use case given the question. Cheers.
这允许在命令行中选择数据库并设置默认值。当然,它会阻止您从命令行覆盖和使用“测试”数据库,但我认为鉴于这个问题,这是一个有点奇怪的用例。干杯。
回答by akostadinov
At the moment (looking 2.6.4) there is no way to universally set the default DB for the client. It seems hardcoded to test
. In my anger I downvoted the Stennie's answer [1] because it doesn't work if your user does not have permissions to access the test database. But if your case is not such, then it may work good enough.
目前(看 2.6.4)没有办法为客户端普遍设置默认数据库。似乎硬编码为test
. 在我的愤怒中,我对 Stennie 的回答 [1] 投了反对票,因为如果您的用户没有访问测试数据库的权限,它就不起作用。但如果你的情况不是这样,那么它可能足够好。
[1] and I can't reverse my vote now unless answer is edited
[1] 除非编辑答案,否则我现在无法撤回我的投票