How to Change the Mongo Default Database from "test"

Introduction

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.

Prerequisites

  • 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.

Goal

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 test database.

> mongo
> db
test

We want 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.

> show dbs
admin         0.000GB
config        0.000GB
demoDatabase  0.000GB
local         0.000GB
peopledb      0.000GB
sample        0.000GB
test          0.000GB

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 /Users/myuser/.mongorc.js

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 demoDatabase, so we use this line of Javascript:

db = db.getSiblingDB("demoDatabase")

5. Verify it worked

Now to verify it worked, we log in to mongo shell again and use db command to show current database.

> db
demoDatbase

It was successful. As you can see, this time when we started the mongo service it already has our demoDatabase selected instead of test.

Other Options

You can also select a database at startup directly from the mongo command by adding the database name after the mongo command like so:

> mongo demoDatabase
> db
demoDatabase

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.

Learn More

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.

Conclusion

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!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.