Skip to content
Snell Rojas

Programming

MongoDB: Getting Started

Get a MongoDB instance up and running with Docker in minutes: what MongoDB is, its pros and cons, when to use it, and a docker-compose file to install it.

Snell RojasPublished Updated 7 min read

I wrote this post in Spanish in 2022 and adapted it to English in 2026.

The goal of this short tutorial is that, by the time you finish reading it, you'll have a MongoDB instance up, running on Docker, and ready to receive data.

It's not an in-depth tutorial: we won't dig into topics like SQL databases, non-relational databases or document-oriented databases.

There's plenty about all that online already. This tutorial is more of a "getting started" guide to help you get a Mongo instance ready as soon as possible so you can start testing.

With that said, let's look at the prerequisites.

Prerequisites for This Tutorial #

  • Docker installed (learn how to install it by clicking here).
  • A basic idea of what a database is.

As you may have noticed, there are hardly any prerequisites.

The truth is, you don't need much to have a MongoDB instance up, listening and ready to store documents.

That said, you can install MongoDB directly on your local machine.

I recommend using Docker, because that way you can install the exact version running on your server.

That guarantees that all the processes you build locally will end up working beautifully once they're in production.

As for the basic knowledge of "what a database is": in this tutorial, to move faster, I'm not going to explain everything related to databases.

Things like their history, the context, why they're used (why Excel isn't a database, lol) and other topics. That's the bare minimum you should know already. Otherwise, what are you doing reading a MongoDB tutorial? Anyway, let's move on to what MongoDB is.

What Is MongoDB? #

MongoDB is a document-oriented database.

That brings a set of pros and cons we'll look at in more detail. For now, just remember that if you're capturing or creating JSON documents, the best thing you can do to store them is use a database like MongoDB, which is optimized for storing documents.

Pros of using MongoDB

Its main advantage is flexibility.

Documents don't all need the same shape, the same number of fields or the same data types to be stored.

In other words, inside the same database (even the same collection) you can have different documents with different shapes, all stored without breaking anything.

We'll look at "collections" in more detail. For now, just remember that Mongo is flexible because it uses "collections" instead of "tables", so it doesn't need fields already created in the database before saving new documents.

On the contrary, the moment Mongo receives a document with fields it doesn't know, it takes in the new fields as if nothing happened.

It even creates the database and the collections if it can't find them because you didn't create them beforehand.

Cons of using MongoDB

That same flexibility I mentioned in the pros brings a few downsides...

To be flexible, Mongo creates collections instead of tables. That's because MongoDB is a non-relational database...

Which means that if you want to search through a large number of documents, that query is going to take a while.

It's going to take forever!

The reason is that Mongo is more of a database for storage than one designed for querying.

This isn't a downside as such, it's just something to keep in mind before trying to use MongoDB as your database.

Another advantage that can also be seen as a downside (that's why I'm putting it with the cons) is creating indexes.

To speed up searches, MongoDB gets by using indexes. In other words, inside a collection in a database, you can create indexed fields so queries on those fields are extremely fast.

What's the catch?

Like with everything, you may need to keep the number of indexes low for your queries to be fast, because if every field is indexed, it's the same as if none of them were.

This will make more sense in the next section, where we look at when to use MongoDB.

When Should You Use MongoDB? #

If you read between the lines in the previous section on its pros and cons, you've probably already figured out when this kind of database makes sense.

Since it's a database where queries are very slow and inefficient, MongoDB is ideal when we want to store data for the long term.

For documents that will be used only once (when they're created), or very, very rarely, MongoDB is perfect: it's lightweight, flexible, fast at storing documents, and you don't have to rack your brain creating relationships between tables.

That's MongoDB's main virtue, and it's the reason you should use it when you're going to store documents you want to keep available long, long term.

How to Install MongoDB with Docker #

Okay, as I mentioned, you need Docker installed on your machine for this part of the tutorial to work.

Instead of chasing dependencies, or trying to get it working right on your machine, I'm going to invite you to use Docker.

I already said this, but I'd like to stress why.

The reason to use Docker is simple: with Docker you can choose which version of MongoDB to install.

That's an incredible advantage, because it lets you install the exact version you have in production (or will have in production). So whether your environment was built before you got there, or you're starting to build it right now, you make sure you have a version that will work every time.

Updates tend to make things stop working, whether by dropping support for features or adding features you don't need. With all that said, let's look at a small docker-compose file that will give us a MongoDB container.

Docker Compose to install MongoDB 5.0.13

You can follow Docker's official documentation, which I'm leaving you just a click away, or follow this tutorial, whichever you prefer.

First of all, get organized: run this command in the terminal:

bash
$ mkdir ~/mongo_tutorial && cd ~/mongo_tutorial && touch docker-compose.yml

The command above creates the mongo_tutorial folder in your home directory, goes into the folder and creates the docker-compose.yml file.

Now open the file with your favorite code editor by picking one of these 3 commands:

bash
$ code docker-compose.yml (recommended)
$ nano docker-compose.yml (advanced)
$ vim docker-compose.yml (for geeks like me)

Once the file is open, copy and paste this snippet:

docker-compose.yml
version: '3.1'

services:

  mongo-tutorial:
    container_name: 'mongo-tutorial'
    image: mongo:5.0.13
    restart: always
    ports:
        - '27018:27017'

This docker-compose.yml will create a Mongo container named mongo-tutorial, on version 5.0.13, listening on port 27018.

To start the service, all that's left is:

bash
$ docker-compose up -d

And that's it!

You have Mongo installed and ready to use on your machine.

If you'd rather install Mongo locally, directly on your machine, here's the documentation so you can have fun: install MongoDB locally.

If you want to delete everything we did in this tutorial, run this command in the terminal:

bash
$ cd ~/mongo_tutorial && docker-compose down -v --remove-orphans

At this point we've covered the main goal of this short tutorial, which was getting Mongo installed.

To add documents, create databases, manipulate documents and so on, you'll need to use an IDE (like DataGrip or Studio 3T), do it from the terminal (like geeks, myself included) or use the always wonderful Python (with the PyMongo library).

In any case, we'll stop here, or the tutorial gets too long and nobody will finish reading it.

Here's the follow-up to this tutorial, which will let you start putting documents into your MongoDB: PyMongo: using MongoDB from Python.