-
Notifications
You must be signed in to change notification settings - Fork 0
MongoDB and the MongoLab Heroku Addon
To have a full stack app, you need to use a database. I used MongoDB.
Here’s what I learned about Mongo today. I was hoping to change my User object on my production site from role: ‘user’ to role: ‘admin’. In my development environment, I am an admin and I need that same capability on the production site to make sure that I can monitor activity.
I began my journey on the terminal, thinking that I could access the DBs from there. Luckily, I already had the mongo shell installed on my machine and had added the <mongodb installation dir>/bin to the PATH environment variable (as is mentioned in this mongo shell tutorial), so I simply needed to type mongo.
The mongo command in terminal allows you to access your machine’s DBs. You can look up which DB you’re currently looking at with db (it will probably respond with test), you can see all of your machine’s DBs with show dbs and you can switch DBs with use <name of DB>.
Once you’ve navigated to your desired DB, you can check out what’s inside. Any object that you store goes into a collection. Use show collections to see all of the collections. They should be things that are obvious and relevant to the application that is using that DB. For me, I saw books and users as the main, obvious collections. I wanted to look further, so I used db.users.help() which gave me a handy list of all of the things that I could do with my book collection.
Now, remember, I’m trying to access a single user and change the role value from user to admin. So, to find that user I searched db.users.find({email: ‘cofauver@gmail.com’}) and to make it slightly easier to read db.users.find({email:'cofauver@gmail.com'}).pretty().
This displayed all of the info in the DB about that user. Surprisingly (or not) I saw that the role here was already ‘admin’. Why? The terminal can only access local DBs, and as I said earlier, I am an admin in my development environment, but I’m not in my production environment. The production DB is not a local DB and we need another way to access it.
Here’s where I went to Heroku’s very helpful dev center site. I knew my Heroku app was connected to MongoDB by the MongoLab addon, but I didn’t have much of an idea of what that addon did. A quick search brought up this page about the MongoLab addon. Even though my terminal work brought forth some useful first touches on manipulating local DBs, MongoLab helped me jump past that raw manipulation and into a clean and simple webapp to change DB entries.
The heroku addons:open mongolab command brings you right to the page. Click on a collection and you can see all of the “documents” (their name for objects) in that collection. You can directly edit them by clicking on the edit document button to the right. BOOM! An amazing addon that I now know more about because of a terminal adventure!