Skip to content

How to Create and Setup a Mongo Database

danielstreit edited this page Sep 4, 2014 · 1 revision

Table of Contents

Setup an Account

I suggest using a hosted Mongo database. I am most familiar with Compose.io, but there are other options. Compose offers a free sandbox database that is great for toying with, but it is notably slower than the paid option and not suitable for production.

Create a Database and Collection

After signing up, follow the directions to create a database and a collection. Our database is called curatedb and our collection is called collections! We currently only have one collection, but you can create more if you need them.

Connection String

Once your database is up, you'll want to connect your program to it. This is done via connection strings. Navigate to the admin section and you'll find the uri to connect to your database. Username, password, and permissions can be set up under the users tab of the admin panel. Once you have a username and password, plug it into your uri. This is your connection string. It should be kept secret. Save it as an environmental variable on your server and refer to that environmental variable with your program. Never commit it to GitHub.

On our server we saved our connection string as CURATES_DB_URI. To access this in node:

  process.env.CURATES_DB_URI

Then, to connect to the server using mongoose:

  var mongoose = require('mongoose');
  mongoose.connect(process.env.CURATES_DB_URI);

Bask in Your Glorious Data

You already know how to do this.