JS Questions: What is MongoDB?

This is the fourteenth in a series of posts I’m writing that aims to provide concise, understandable answers to JavaScript-related questions, helping me better understand both foundational concepts and the changing landscape of language updates, frameworks, testing tools, etc.

What is MongoDB?

MongoDB is an open-source database program. There are several database management systems; some of the most popular are Oracle, MySQL, and PostgreSQL. MongoDB has been gaining popularity in recent years and is different from the ones above, because it’s a NoSQL, or non-relational, database management system.

First, Relational Databases…

Relational databases have rows of data stored in tables. A row of data can contain columns with keys that link one record to another. For instance, if there were a relational database containing data about the show Comedians in Cars Getting Coffee, it may have a table for episodes and another table for comedians. Every episode features one comedian (aside from the comedian host, Jerry Seinfeld) so each episode record could have a column called comedian_id that contains a key corresponding with a comedian from the comedian table.

Below is a visual showing how the data would be stored. The only episode in the episodes table has a comedian_id of 2. Kristen Wiig is the comedian in the comedians table with the primary key 2, so she is the comedian for this episode.

Screen Shot 2018-01-22 at 10.47.32 AM.png

Using the database querying language, SQL, tables can reference each other’s data through JOIN statements. Here’s an example:

SELECT Episodes.Title, Comedians.Name FROM Episodes INNER JOIN Comedians ON Comedian.Id = Episodes.Comedian_id;

This is saying: show episode titles with their corresponding comedian names by using the episode’s comedian_id to find the comedian with an id of the same number.

Now, NoSQL Databases…

Records are stored differently with MongoDB. Instead of table rows, data is stored in JSONlike documents.

A document can contain nested information. For instance, an episode document may contain a nested comedian document. Or, a document for the show’s seasons could have a nested array of episodes.

Below is an example of what a document might look like for an episode with one comedian.


A grouping of documents–all comedians, for example–is called a collection. It’s the equivalent to a table in a relational database, though unlike a relational database, items do not need to have the same schema.

The mongo shell can be used to interact with a database and perform actions, such as retrieving all documents from a collection. The below code would grab all comedians from a database (db) with a comedians collection, using the .find() operation:


Since documents can store nested data (such as an episode’s comedian), the need to perform a costly ‘join’ in order to access data is reduced. (MongoDB does have the functionality to join collections when needed.)

MongoDB in context…

MongoDB is generally associated with the latest wave of web applications and tech startups. Of course, different database management systems are suitable for different projects, but these are a few reasons (I think) MongoDB is gaining momentum.

MongoDB is well suited to the Agile style of software development that has become popular. Engineers using a quick, iterative development approach can easily change their schema and models as needed.

For startups and fast growing applications, there’s also the benefit of MongoDB’s distributed database design. Data is stored across multiple machines by default, so the database can easily expand as needed.

The 2017 Stack Overflow Developer Survey has a correlated technologies graph showing MongoDB closely linked with the frameworks Angular, Node.js, React, and Cordova, as well as the languages, JavaScript and TypeScript (a syntactical superset of JavaScript). While it’s not necessary to pair MongoDB and JavaScript (there are several ‘drivers‘ that can be used to interact with MongoDB with different programming languages), building full stack applications entirely with JavaScript is growing in popularity. Two common stacks for building modern web applications are MEAN (MongoDB, Express, Angular, Node.js) and MERN (MongoDB, Express, React, Node.js).

Part of the excitement about MongoDB can also be attributed to the fact that it’s new, has an excited community, growing features, and pretty approachable documentation and educational features on the MongoDB site.

Final note: There’s a lot of debate about whether MongoDB is better than SQL options. MongoDB may be faster in some cases (such as the previous example of reading an episode’s comedian), but SQL databases have their own advantages, including the reliability of a mature system (Oracle has been around since 1977). This Sitepoint article offers an introduction to best use-cases for SQL and NoSQL databases.