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?
相关问题
- MongoDB can not create unique sparse index (duplic
- How to get the return code of a shell script in lu
- Spring Data MongoDB - lazy access to some fields
- Golang mongodb aggregation
- Invoking Mirth Connect CLI with Powershell script
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- mongodb-aggregate聚合查询分组后如何获得多字段
- In IntelliJ IDEA, how can I create a key binding t
- mongodb error: how do I make sure that your journa
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- How to track MongoDB requests from a console appli
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:
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')
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.
Command Line
You can select the database to use on the mongo command line, eg for 'mydb':
If a database name is not provided, 'test' will be used.
In .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:The
.mongorc.js
file is executed after themongo
shell is started, so if you set a default here it will override a database specified on the command line.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.[1] and I can't reverse my vote now unless answer is edited