Programming With Jon Logo

Programming with Jon

Why MongoDB?

October 14th, 2020

[object Object]

What is MongoDB?

I am sure that most of you have heard about what MongoDB is. For those that have heard the name and thought "What is that?" MongoDB, or Mongo for short, is a document database that uses JavaScript as it's query language. So, for instance, SQL is a query language that allows you to interact with a database to retrieve data stored in tables using SQL. Well Mongo is similar to SQL in that it is a database that allows you to interact with data stored inside. The difference being that you use JavaScript to interact with the database much like you would use SQL to interact with a database in PostgreSQL.

Collections or Tables?

Mongo uses what are called collections to store the data in the database. The data is stored in documents of JSON. JSON stands for JavaScript Object Notation.

{

name: 'Jonathan'

}

Which is pretty great for us developers that need to interact with said data to display it on say a webpage or web app. Since JSON is the preferred way to send and receive data in a web app. For an even better example, a blog application may have a collection for posts, another for the users of the blog site, and quite possibly a third for say comments. If we compare a collection to a JavaScript object, it would be the top-level object, while documents are the individual objects within. It would look similar to the following:

collection: {

document: {},

document: {},

document: {}

...

}

We can create a simple document in our database by using a few commands. I like to use the mongo shell periodically when I just trying out commands. But if you want a good GUI for managing mongodb's I use Robo3T which you can get from here. But obviously without having MongoDB installed you aren't going to be able to play around with creating and manipulating data and databases.

Installing MongoDB

For those of you on macOS you can use Homebrew:

brew update

brew tap mongodb/brew

brew install mongodb-community@4.2

brew services start mongodb-community

This is simply reaching out to find mongodb and installing the community version of MongoDb at version 4.4. Then after that has finished we are simply starting the MongoDB service so that it is running in the background of your machine.

For those of you on Windows you will need to first download the installer from MongoDB Download Center. Once the file has downloaded, run the installer and follow the prompts. Select the Complete setup type. You can leave the defaults. I will mention in the installer there is a check box at one point in the lower left corner of the install wizard window asking if you want to install Compass. Compass is another pretty awesome GUI for interacting with your MongoDB's and the data stored within them but I prefer Robo3T. In the end try them both out and pick the one you like. To verify that MongoDB was installed and to start the service follow the steps below

  1. Locate the Windows Services console

  2. Find the MongoDB service

  3. Right-click the MongoDB service

  4. Click start

Adding the Shell to path

If you are on Windows then you are most likely familiar with adding something to path in order to access it globally from your Terminal. It can be frustrating but I will walk you through it. First we are going to open up cmd/powershell/windows terminal etc whichever one you are using, and type in

mongo

If successful you should see:

[object Object]

If that is the case move on to the next section to start creating a database and some data. If not then you will need to follow the steps below:

  1. If you left the defaults when installing Mongo and didn't change the installation directory you can copy this path: C:\Program Files\MongoDB\Server\4.2\bin

  2. In the search in the bottom left by the start button type envi that should bring up the edit system environment variables option, click that

  3. [object Object]

    Click the button the arrow is pointing to above

  4. [object Object]

    In the bottom half of this window highlight the path like the arrow shows and click edit

  5. [object Object]

    Click New at the top and in the field that is created paste in the path you copied from step 1.

  6. Click Ok

  7. Click Ok

  8. Click Ok

After following those steps you should now be able to open a new terminal and type in the mongo command to see the successful message from above and you are now ready to create a database.

Quick note - You might have to restart these steps if you restart your machine.

Creating Data and a Database

Now that we have Mongo installed and the service is running in the background of our machine we can create a database and start creating some documents to see how easy it can be to work with Mongo. Open up your terminal/command prompt and type:

mongo

// create and switch to database

use streetfighter

db.fighter.save({ name: "Ryu" })

// if successful

WriteResult({ "nInserted" : 1 })

// we can write multiple entries into the db at once

db.fighter.save([{ name: "Chun Li"}, { name: "Cammy" }, { name: "Guile })

Now that we have some documents written to our database, let's retrieve them. To do this, we'll use MongoDB's find method.

db.fighter.find()

// This should retrieve all four entries that we stored like so

{"_id": ObjectId("<id number here>"), "name" : "Ryu" }

{"_id": ObjectId("<id number here>"), "name" : "Chun Li" }

{"_id": ObjectId("<id number here>"), "name" : "Cammy" }

{"_id": ObjectId("<id number here>"), "name" : "Guile" }

We can also find the individual documents by both property values as well as Mongo's assigned ID

db.fighter.find({ name: "Ryu"})

db.fighter.find({ _id: ObjectId("id here")})

These are pretty common ways to look for items stored in Mongo.

Conclusion

I didn't want to cover every aspect of Mongo in this post as it was just meant to be an introduction to the database. I highly recommend downloading the database and just playing around with the commands I have shown here. If you are really interested in learning more I would definitely say head over to the Official Documentation page and read about the other commands. Maybe you want to update the fighter here from Ryu to Ken or Guile to Akuma? Maybe you want to remove one of the fighters altogether. The documentation is a great place to start. Hope this helps show the simplicity of the database and also give some insight as to why you would choose Mongo for your next project. Happy Coding.

Created by Jonathan Reeves, © 2020