Multi-Environment MongoDB

Mike Wilcox
4 min readAug 4, 2018

Recently, while building a side project, I came to a cross road where I needed to decide how I was going to persist some user session data over time. I opted for the database route and discovered a manageable path for both local and cloud storage. In this post I’d like to walk you through the steps I took to get there.

First, a little bit about the project that I am working on. I am building an app that is an extension of the Spotify desktop app. For this app, I am using their provided API to fetch various types of music information. A requirement for the app is the user can log in using their Spotify credentials. For these details, I chose a database to persist the minimal amount of session information so that the auth flow works smoothly using tokens.

For this use case, I am running express on a NodeJS server. I chose to use MongoDB for the database. First, you’ll want to have MongoDB installed locally. Check out their official installation documentation for your platform of choice. Once installed, you should have access to the mongod executable from the command line. Running mongod will start the mongo server locally.

Note: when running mongo locally, you may want to use a GUI to inspect what is being stored into the database. I use Robo 3T as it works quite well for my use case.

Next, we’ll want to get setup on the cloud storage side with mLab. You’ll want to sign up or log in to their service. Once in, we’ll need to create a new database.

mLab — create a new database

Next, you’ll be presented with a screen to choose the cloud provider and plan type. I chose AWS and Sandbox to begin with. Feel free to choose what you think works best for your use case.

provider and plan selection

After this you should see a screen to select the database region and enter the name of the database. Once completed, you should then see the database listed in the list of MongoDB deployments on your dashboard.

database creation success

Click on the database that you just created. We need to create a user for this database. This will be a username and password that will allow us to connect to the mLab service from our application. Don’t worry, we’ll have these credentials stored in environment variables on the machine. They’ll never be exposed in production code.

Add a user to the database!

With the new user successfully created, take note of the MongoDB URI string that they are providing. We will need this string when using this database in production. For this use case we will need three environment variables to make this work:

  1. NODE_ENV environment the app is running: for my use case dev or production
  2. MONGO_DB_URI_DEV local mongo address
  3. MONGO_DB_URI_PROD cloud mongo address (mLab)

We are going to access these variables in code, if you need some help with how to set these up — check out this handy guide for adding them on your preferred platform.

To connect to the mongo database from my node app, I use the mongoose package.

mongo environment toggle

In this snippet of code, the mongo database connection is established based upon which environment the app is running. If the app is in production, then the app will use the mLab URI. Otherwise, it will connect the locally run mongo.

This approach has a couple of advantages. Running this on production becomes a lot easier if you’re not worried about having a third party manage your data. You still have the comfort and peace of mind of having mock data accessible from a local source while in development.

If you’d like to see the code, here is the commit where I added mongo environment support.

Happy Hacking!

Header Photo Credit

--

--