MongoDB

MongoDB is a NoSQL document database for high volume data storage.

MongoDB uses collections and documents for its storage. Each document consists of key-value pairs using JSON-like syntax, similar to a dictionary or JavaScript object.

Likewise, as MongoDB is a NoSQL database, it uses its own query language, Mongo Query Language (MQL) which uses JSON for querying.

Getting Started

Installation

MongoDB can either be installed locally following the instructions here or you can create a remotely-hosted free 512 MB cluster here. Links to videos with instructions on setup are at the bottom.

This tutorial assumes that you have the MongoDB Shell from here. You can also download the graphical tool, MongoDB Compass, down below from the same link.

Components

After installing MongoDB, you will notice there are multiple command line tools. The three most important of which are:

  • mongod - The database server which is responsible for managing data and handling queries

  • mongos - The sharding router, which is needed if data will be distributed across multiple machines

  • mongo - The database shell (using JavaScript) through which we can configure our database

Usually we start the mongod process and then use a separate terminal with mongo to access and modify our collections.

JSON & BSON

While queries in MongoDB are made using a JSON-like* format, MongoDB stores its documents internally in the Binary JSON (BSON format). BSON is not human readable like JSON as it’s a binary encoding. However, this allows for end users to have access to more types than regular JSON, such as an integer or float type. Many other types, such as regular expressions, dates, or raw binary are supported too.

Here is the full list of all types that are supported.

  • We refer JSON-like to mean JSON but with these extended types. For example, you can make queries directly with a regular expression or timestamp in MongoDB and you can receive data that has those types too.

Further Reading

Setup Videos

Input Validation

From the examples above, if input validation or structure is a concern, I would take a look at the following ORMs:

  • Mongoose (Node.js) - Input validation through schemas that support types, required values, minimum and maximum values.

  • MongoEngine (Python) - Similar to Mongoose, but I found it somewhat limited in my experience

  • MongoKit (Python) - Another great alternative to MongoEngine that I find easier to use than MongoEngine

For statically strongly typed languages (e.g. Java, C++, Rust), input validation usually doesn’t require a library as they define types and structure at compile time.

Resources

If you have the time to spare, I would strongly recommend the courses on MongoDB University. They’re by MongoDB themselves and go into much more detail while still being concise. They’re a mix of videos and quiz questions and this was how I gained my knowledge of MongoDB.

I would recommend the following video series for learning MongoDB:

Language-specific ones that I used before:

Most of the information above was cross-referenced with the MongoDB docs. Here are the docs for each section:

If you’ve been enjoying MongoDB so far and want to explore intermediate features, I would look at aggregation, indexing, and sharding.

  • Aggregation - useful for creating advanced queries to be executed by the database

  • Indexing allows for caching, which allows for much faster execution of queries

  • Sharding allows for horizontal data scaling and distribution between multiple machines.

Last updated