Why MongoDB
Tags: javascript, mongodb
Published: 2020-10-15 16:26:45

# 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. It looks like:
```json
{
    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: 
```js
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 am just trying out commands. But if you want a good GUI for managing MongoDB collections and databases I use Robo3t which you can get [here](https://robomongo.org/download). But obivously 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:
```bash
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 edition at version 4.2. Then after that has finished we are simply starting the MongoDB service so that it is running in the background.

For those of you on Windows you will need to first download the install from [MongDB Download Center](https://www.mongodb.com/try/download/community). 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 instances. I still prefer Robo3T but it might be a good idea to play around with both to see which one you like better. To verify that Mongo 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 MonogDB service
4. Click start

## Adding Mongo Shell to Path on Windows
For those of you on Windows, you might have run into a problem with running the mongo command if you went through this post before I made this edit. I am sorry about that. The reason behind that is because you haven't added the path to the mongo shell into your system path environment variables. So We are going to do that here.

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 start typing envi that should bring up the edit system environment variables option, click that.
3. Click on the Environment Variables button 
4. Select the Path option in the second half of that screen
5. Click Edit
6. Click New
7. Paste in the copied path from step 1.
8. Click Ok three times

Close any terminal/cmd/powershell etc windows you have open and open a new one. Once you have a new one open type in the mongo command. You should be greeted with a message and the prompt should change to '>'. You should now be able to move on to the create a database section. 



# 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:
```bash
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

```bash
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

```bash
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 [Official Documentation](https://docs.mongodb.com/) page and read about the other commands. Maybe you want to update the fighter here from Ryu to Ken or Guile to Akuma? Or 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.