Sunday, June 11, 2017

Learning MongoDB from MySQL Background




Until this point, I've only had experience with using MySQL databases, but I've been wanting to try MongoDB because it has a funny name and also because it's widely praised.

The truth is, if you're skeptical there are many reasons to give MongoDB a shot:
  • Its JSON-like syntax and ability to chain commands is pretty sweet!
  • It can make your web app/website more efficient in the right circumstances
  • If you're using node, you've got access to some nice Mongo drivers

Now I know there are a billion better articles out there about using Mongo, this article is my experience as a self-taught mysql guy, and I hope I can provide a general outline and some useful resources so you, too can start Mongoing around.

As usual, I decided to learn by doing, so I've started a new project. I call it "scorecard". It's a simple application for score-keeping (mind... blown. I know.) To do this, my first step was to understand what the heck all this "mongo" stuff was about.

Step 1 - Understanding MongoDB in MySQL terms

It took me a little while to fully wrap my head around the structure of MongoDB and it wasn't until I started comparing it to MySQL that I full understood. Here's my way of understanding it:

Just like MySQL, you can host multiple databases on your machine. Within those databases, MongoDB stores things in collections, which are basically like tables in MySQL. Within each collection is a document, which can be thought of as each row in MySQL, as it stores all the values of a particular record.

So in summary:
  • database = database
  • collection = table
  • document = row

The difference of course is that MongoDB is far more flexible. Each document could look entirely different from others within the same collection. This is the key to understanding when MongoDB may be more efficient, or more cumbersome for your application.

For my application, in each scorecard collection, there will be 0-100 players, a title, some meta information, and possibly extra data if say the scorecard is for a golf game where the score should be kept differently.

In this case, if I were using MySQL I would need to create columns for all this data, likely on several different related tables, and it would add a lot of complexity to a rather simple application. Using MongoDB, I can nest each player within each scorecard document, and have any unique number of attributes that define settings for each unique scorecard. Later on, I may want to add complexity to my players, and break them out into a related collection, but for now I'm keeping it simple.

 

Step 2 - Installation

The simplest way to install MongoDB on a Mac is to use Homebrew. The MongoDB Docs actually provide a very easy to use, step by step instruction of how to install on both Mac and PC.

 

Step 3 - Running Mongo Daemon and Mongo Shell 

To start using mongo, you have to first run Mongo Daemon in the background by typing mongod into your command-line.

Mongo Daemon looks like a bunch of jibberish, and talks about itself a lot. Basically, it's your mongo server that makes the magic happen, just let it talk to itself in another terminal window.

Next you can start playing with mongo in your command-line by typing mongo. (Note, if you've manually installed mongo (like, not with homebrew), you may need to do this while in the directory you downloaded the files to).

Step 4 - Using Mongo Shell Commands To Create Test Data

Let's go over some basic commands to get you started, but you know the MongoDB docs are great for this.

These are the commands that have been useful for me so far. There's so much more to learn, I recommend doing some tutorials and going through the docs to keep playing around.

Now that I've got my database setup, I've started to create a node/express backend API, and a React front end for my scorecard app. I'll post about that stuff next time!

Happy coding y'all!

No comments:

Post a Comment

Learning MongoDB from MySQL Background

Until this point, I've only had experience with using MySQL databases, but I've been wanting to try MongoDB because it has a fu...