How to Change the Mongo Default Database from "test"
When you start the MongoDB service it defaults to using the
test database which can be an annoyance because you have to switch over to the database you are working with every time. We encountered this annoyance ourselves and wondered if there was a way that it could default to a database that we use most often, and in fact there is. The process of doing it was more complex than we expected so we wrote this tutorial in case there were other MongoDB users who have dealt with this same problem and wanted to find a quick solution. What follows is a step-by-step tutorial of showing you how to change the default database.
- You should have MongoDB installed and running.
- It’s recommended that you create a database and collection to experiment with as you follow along.
- Some command line experience is recommended.
We always like to have a clear goal when we start a tutorial. Our goal for this tutorial is to change the default databsae from
test to our
demoDatabase that we work with daily. We will accomplish this using the MongoDB shell.
1. Identify the problem
Let’s show the problem before we begin solving it. When we startup mongo and use the
db command to see the current database and it is using the
demoDatabase to be used instead because this is the database we used most frequently.
2. Identify the database we want to use exists
The next step is we want to make sure that the database we want to use as our default exists so we’ll execute the
show dbs command.
As you can see, the
demoDatabase does in fact exist.
3. Find .mongorc.js file
Now we want to find a hidden file called
.mongorc.js file is typically under your home directory. We are on a mac so ours is located at
Note: This is a hidden file so make sure you are not missing it. Use
ls -a in Terminal to see all hidden files.
This file automatically runs when a user logs into the mongo shell and you can set your default database at that time.
4. Modify .mongorc.js file
Now that you’ve found your
.mongorc.js file we can add a line of code that will set default database to your db, ours is
5. Verify it worked
Now to verify it worked, we log in to mongo shell again and use
db command to show current database.
It was successful. As you can see, this time when we started the mongo service it already has our
demoDatabase selected instead of
You can also select a database at startup directly from the mongo command by adding the database name after the
mongo command like so:
This is a good alternative to .mongorc.js and it might be good if don’t want to specify defaults in a hidden file that you are worried you won’t be able to find later.
To learn more about the
.mongorc.js we suggest you look up the MongoDB documentation directly or you can always reach out to an expert at Object Rocket to discuss your specific application.
We showed you how to change the default database using the MongoDB shell. We hope this saves you some time and a few keystrokes. We hope this tutorial was able to answer your question or provide you the snippet of code that you were looking for all along. Thank you for your time and if you have any feedback or questions do not hesitate to reach out to us.
Pilot the ObjectRocket platform free for 30 Days
It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!PILOT FREE FOR 30 DAYS