Anyone who has ever worked with databases that organize data into tables, also called relational databases, can probably speak at length about its flaws. As great a system for storing data it may be, it is not without its issues and is not designed for interpretation by people, making organizing with it convoluted, redundant, and with a steep learning curve.

Hence, the NoSQL movement. SQL stands for Structured Query Language, and it is used to communicate with databases. But it isn't that great on a larger scale. When, for example, apps go viral, support is needed so the users can use the app without experiencing any issues. But SQL gets complicated at this scale, more so than many developers can handle.

NoSQL makes storing and organizing data easier, because proponents of the NoSQL school of thought strived to make a database that worked with large data sets, scaled, and was organized like the way people think, and could be easily read by humans. MongoDB is one such database, and it's changing the way developers build websites.

What is MongoDB?

Connection_to_the_MongoDB_Shell

MongoDB (from "humongous") is a cross-platform document-oriented database used as an alternative to MySQL. But what does that mean? Well, a database is a structured set of data, however MongoDB differs in that it tends to be less strict about how that data is structured. Information is grouped together, rather than broken down into related documents and tables like MySQL, a popular RDBMS, or relational database management system, that does use SQL.

Many databases that use SQL store data in tables on pages. Think of something like an Excel spreadsheet with bits of code. These pages are the smallest pieces of data on a server, and each page is devoted to one table. This gets convoluted at the large scale, whereas with MongoDB, the developer can decide how to structure the information for whatever suits their needs.

MongoDB uses BSON to store information. BSON (a combination of "binary" and "JSON", or JavaScript Object Notation) can be thought of as a binary or numerical representation of JSON documents. JSON is an open-standard format (like open-source) for organizing data.

It also adds support for certain data types that aren't supported in JSON, like the data type 'date', which wouldn't normally be supported and therefore could not be used by the developer. The developer, in this case, wouldn't be able to include any sort of date as a piece of data.

Typically, this data is organized into documents. JSON transmits data using documents that can be easily read by humans, however this is not the case with BSON. JSON is also commonly used to transmit data between a server and application, as well as a general data storage medium.

Documents do not need to be structured in a specific way according to the collection, or a group of related objects they're organized in, making it very easy for a developer to get up and running with MongoDB. This has lead to it being one of the most popular NoSQL databases out there.

What Makes It Different From MySQL?

Mysql-screenshot

With MySQL being perhaps the most widely used RDBMS out there, although flawed as it may be, it's only natural that alternatives would crop up. With NoSQL being an alternative to SQL, MongoDB is considered an alternative to MySQL. MySQL uses tables to organize its data and is based off the classic relational data model from the twentieth century, although, like MongoDB, it can be modified for improved functionality.

However, the two differ greatly. They're intended for different purposes, for one. MongoDB is intended more for the larger (very large) scale whereas there's only so much that MySQL can handle. As was mentioned before, MySQL uses tables to organize data which can end up very convoluted. However, they are both very good systems, and in fact, hybrids are being implemented on certain sites around the web. Craigslist is known for being one of the more successful websites implementing a MongoDB/MySQL hybrid.

How Does It Work?

mongodb document

MongoDB works by storing data in documents similar to JSON. These documents have one or more fields (objects or values contained within a class or structure) which include arrays (a series of objects all the same size and type) and sub-documents (a document that makes up part of a larger document). These fields can be set as the developer so wishes, according to their needs.

While organizing this would normally be a complicated affair, BSON makes it easy by simply grouping the documents together according to type and size rather than breaking them down into very small pieces of data like SQL does and organizing the data based on other information. Because of this flexibility, developers are able to evolve the collection of data, also called the data model, to suit their needs.

Developers can access the documents through drivers that they are able to read and understand, and the documents are available in most popular, modern programming languages. With MongoDB, the documents are mapped naturally to the objects so there's no need for an ORM (object-relational mapping) layer that manually maps documents to their respective objects. Simply put, the documents are connected to the objects naturally, so there's no need for a separate command or program to connect them.

MongoDB uses a JavaScript-based shell, and supports developers writing custom code using JavaScript functions, and will likely become more widespread as JavaScript continues to boom in popularity.

Who Uses It?

Although still relatively young, MongoDB is used in a number of popular websites. These include business journal Forbes, as well as gif-tastic Buzzfeed.

Forbes took advantage of MongoDB to rapidly develop a new web and mobile portfolio. The speed and power of MongoDB made it possible for them to set up a website in two months and a mobile app in one month. This had a drastic affect on their reader engagement and traffic. According to the MongoDB developers:

Overnight, mobile traffic jumped from 5% to 15% of Forbes.com total traffic, and quickly ramped to 50%.

Buzzfeed suffers from the challenges any large media organization faces. Their distributed writing staff publish such a prodigious amount of content, to a huge audience. MongoDB allows Buzzfeed to rapidly scale according to the traffic they receive, as well as determine what types of content resonates with their readers.

How Do You Use It?

MongoDB is a great tool for web, games and software developers. We know that it's fast, and scalable. And we know that it's easy to read, and to learn. But how do you actually use it?

mongodb shell in browser

The official MongoDB website offers users the opportunity to try a MongoDB shell in their browsers. TutorialsPoint offers several very detailed tutorials for using MongoDB, ranging from beginner to advanced. There are also projects on GitHub related to MongoDB, including The Little MongoDB Book by Karl Seguin, for just about everything you need to know about MongoDB.

If you just want to learn how to install MongoDB, be sure to check out this tutorial from the official MongoDB website.

Should You Use It?

That depends on your individual situation.

As great as MongoDB is, it's not without its flaws. It's not ACID compliant. ACID, standing for Atomicity, Consistency, Isolation, and Durability, is a set of properties that ensure that database transactions, or work performed within a database, is processed and completed correctly. NoSQL databases are sometimes not ACID compliant, which does limit their productivity and dependability.

MongoDB also uses all available memory as a system cache. As a result, there are performance issues on computers running on 32-bit systems, which can only address a maximum of 4GB of memory. To learn more about making use of the RAM on your computer, be sure to check out this article by Brad Jones.

Because there is no need for tables with this database, there's a high risk of developers being lazy and writing sloppy code, however that risk applies to almost anything. You end up with duplicate data, because there is no possibility for joins within the system.

Nevertheless, MongoDB is a blazingly fast, efficient system that can absolutely aid in increasing developer productivity

MongoDB is also user-friendly in that it doesn't require a unified data structure for all the objects, although it is recommended to set one up for ease of use. The structure of the data doesn't have to be the same throughout the system, so a singular voice is not completely necessary.

Finally, according to MakeUseOf writer Matthew Hughes, the MongoDB team runs an excellent blog, featuring high-quality, well-written technical content aimed at a technical audience.

Conclusion

If you don't have a system with a good amount of RAM that can't support a large database, or you're not really looking to have a large database at all, MongoDB is not for you. It tends to only sell itself when at a large scale. For smaller projects, using RDBMS is probably easier. If, however, you need speed and ease and can support the system, perhaps it's worth looking into.

Do you have any experience with MongoDB? Curious about trying it out? Leave a comment below and we'll talk!

Image credits: "Connection to the MongoDB Shell" by Ularugeanina via Wikimedia Commons, "Mysql-screenshot" by Stephantom via Wikimedia Commons