39

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?

Holger Sindbaek
  • 2,278
  • 6
  • 41
  • 68
  • use this to add default database in connection string: https://stackoverflow.com/questions/22418052/connect-to-a-specific-database-by-default-in-mongodb/67114084#67114084 – KushalSeth Apr 15 '21 at 18:27

3 Answers3

52

Command Line

You can select the database to use on the mongo command line, eg for 'mydb':

mongo 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:

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.

Stennie
  • 63,885
  • 14
  • 149
  • 175
  • 2
    I meant like setting a default database to use, so I wouldn't have to start up in test each time. I've set an alias in my .bash_profil now though. – Holger Sindbaek Jul 04 '12 at 23:55
  • 4
    I didn't realize the impact of .mongorc.js. Be careful when using .mongorc.js because when you connect to other database, like your production ones, it will still run the same commands. – Kirk Jan 03 '14 at 19:42
  • 2
    @kirk: FYI, you can run `mongo --norc` if you don't want `.mongorc.js` to be run when the shell starts. – Stennie Jan 03 '14 at 20:07
  • 1
    -1, if you don't have permissions to access database `test` then you are kicked out on authentication before `.mongorc.js` kicks in. – akostadinov Oct 10 '14 at 14:42
  • If you are using mongosh, edit .mongoshrc.js instead. – Ng Lok Chun Aug 11 '22 at 03:32
5

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:

// 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')

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.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
bradq
  • 51
  • 1
  • 2
1

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

akostadinov
  • 17,364
  • 6
  • 77
  • 85
  • Where is the 'test' db as the default database hard coded ? i tried **db = db.getSiblingDB("mydb")** in the root/.mongorc.js but not working – KTM Sep 20 '18 at 09:57
  • 2
    @Crazyrubixfan Apparently it is right in the mongo shell executable itself: `strings /usr/bin/mongo | grep 'dbName || "test"'` shows multiple instances. – Peter Hansen Jan 03 '20 at 19:45