What is Mongo DB? MongoDB is an unstructured database that is becoming extremely popular for speed, ease-of-use, and scalability.
The Problem with Structured Data
Until now, the majority of the my content has covered relational databases. You communicate with a relational database using SQL (Structured Query Language). A relational database follows a structure defined by columns in a table.
The problem with a structured database is that it can become too structured and require a lot of resources to create and design a good database. For instance, I have a fifty part YouTube series just on how to design the structure of a relational database, and that doesn’t even talk about how to use the database.
Just to show one example of this, what if we have a table that defines some structure and for a specific row we want to have some extra data?
This is not easily done with a relational database.
A solution is a new type of database…NoSQL (Not only Structured Query Language). A NoSQL database is also referred to as an unstructured database. Like I mentioned, there are tons of different types of databases, but MongoDB is a specific kind of unstructured database called a document database.
Now don’t think that unstructured data should completely replace structured data. Structured data is still a great thing. Instead, think of unstructured data as being an addition to your structured data – as something that augments your development experience.
A document database has a list of key-value pairs. We can literally redraw our data using key-value pairs rather than having our data be rows inside of a structured table (You’ll see what I mean in a moment). In database terms, a key is known as as a field or attribute. In a moment we will draw out a database structure using MongoDB syntax.
Defining a simple structure
The term document is going to come up a lot when working with MongoDB. A document is a record (sort of like a row in a structured table). For example, if we have a database for users, a document would be one individual user.
Now, a document has a very specific structure. We will start off with a very basic structure.
Here we have fields and values.
As you use NoSQL more and more, it becomes a way of thinking and you are able to think outside of typical structure.
Now, a document can actually get pretty complex, and there are a lot of things you can do with one. This is just the bare minimum.
By the end of this article, I want you to understand what a document is and know how to work with one. Our goal is to take a document, throw it into a MongoDB database, and get that data back out.
Downloading and Getting Started
For a more indepth guide, follow this link.
…Or follow these steps as a general guideline:
1. Download MongoDB.
2. Click recommended installation.
3. When you extract the download, there is going to be a bin folder. Inside of this bin folder there are two important files: mongo, and mongod. Mongod is the server, mongo is how we communicate to this server.
4. We are going to need to create a new folder: \data\db. You can do this in a terminal using:
Alternatively, you can create this new folder using the Finder/Explorer.
5. Once we have this folder created, we can open mongod to start the server.
6. Next, open mongo and this is where we will run our commands against our MongoDB database.
If you receive any pop-ups about security, you’ll probably want to click private network only and not public network.
MongoDB is now installed, and we are connected to the database. You may want to make MongoDB a service (so that it runs behind the scenes). Refer to the installation guide linked earlier for more detailed instructions on how to do this. Setting MongoDB up as a service will ensure that the database starts right away when your computer turns on. If you don’t do this and you want to use MongoDB in the future, you’ll have to run mongod again to get it started.
Creating a Database
To view available databases, run this command:
Run this command to create a database:
This will create the database and set it to be the active database.
Collections and Inserting Data
A collection is like a table. It is literally just a grouping–or collection–of documents. This allows us to organize and group our data.
You have the option of explicitly creating a collection. Or what you can do is just insert a document into a collection and if it doesn’t exist, a collection will automatically be created.
You can see that when you do this, it returns this insertedID back. This is a unique identifier for this document.
The cool part is that we are not restricted to this structure (only these keys). We can actually insert another document but with a different structure:
This is not possible with structured data. If you tried to add a field to only one row with a relational database, you would get an error. The problem is a result of not having the appropriate column defined in the table structure. You would have to update the table structure in order to add a field.
Querying Our Data
Now that we’ve inserted some data, let’s get that data back out.
In SQL, this is equivalent to:
If you want to query the data but you don’t necessarily want to select all of the data, there is a way you can set some rules:
To make the results a little easier to read, you can also use:
In SQL, this is the equivalent to:
Before you go, I wanted to show you a tool that will allow you to easily manage a MongoDB Database. That tool is called ClusterControl. Severalnines, the creators of ClusterControl, breaks database management into four parts:
ClusterControl is designed to help you do these four things seamlessly. The cool part is that this can integrate with other database systems, MySQL and Postgres. Give it a try and let me know how you like it!
The goal of this post was not to make you a MongoDB master. Rather, my goal was to introduce you to the key features of MongoDB and help you see it as a tool you can use in certain circumstances.
Yes, I think it is important to learn the in-depth details, but that is something more suited for a long, in-depth video series. Let me know if that is something you guys would like to see!
My closing thoughts are that you should at least Give NoSQL a try. The way the world works with data is changing and if you are not ready for those changes, you are going to be behind.
If you are a developer, I encourage you to try to build a small app with MongoDB.
If you are running a technology company, know that the benefits of MongoDB and NoSQL in general shine when you realize that the structure of data can change over time and that using a NoSQL System allows you to scale seamlessly. It is important to wrap your head around it in order to future-proof your career and/or your business.
Like I mentioned earlier, NoSQL is not replacing SQL. The reality is that there are things that relational databases do better than non relational databases.
- Begin reading more about relational databases.
- Do research on relational vs non-relational databases (performance, capabilities, cost, etc).