This article’s goal is to quickly set up a MongoDB environment: running MongoDB on the server side, and having clients access it over the network. The server runs MongoDB with Docker, and clients connect and access MongoDB with pymongo.

This article’s goal is not to dive deep into MongoDB’s technical details, but to quickly set up a tool to support later data analysis. It covers three parts:

  • Installing and configuring MongoDB with Docker on the server
  • Connecting to MongoDB from the client with pymongo
  • Backing up and restoring MongoDB

1. Installing and Configuring MongoDB on the Server

1.1 Docker configuration on the server

Installing and configuring MongoDB with Docker avoids tedious MongoDB configuration and won’t mess up your local environment. See Docker’s official site for an installation guide, or see this article. To improve the experience of running MongoDB with Docker, the following Docker configuration is recommended:

  1. Configure a domestic image mirror (using USTC’s mirror here)

    Edit the config file /etc/docker/daemon.json and add:

    {
      "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
    }
  2. Configure Docker to run as a non-root user

    1. Create a docker user group
    2. Add the current user to the docker group

1.2 Running the MongoDB container

Pull the MongoDB image on the server:

docker pull mongo

Start the image (named LearnMongo), noting the following rules:

  1. Port forwarding The MongoDB service uses port 27017 by default; this port needs to be exposed to the host machine when running with Docker.
  2. Storage location MongoDB stores data at /data/db; this needs to be mapped to a folder when running with Docker.

The full startup command is:

docker run --name LearnMongo -d -v $HOME/.LearnMongo:/data/db -p 27017:27017 mongo

Check whether the container started up with:

docker container ls -a | grep LearnMongo

If there’s output, it’s running normally; if not, check whether Docker itself is running properly.

On the client machine, check the server’s relevant port to make sure the connection works:

curl localhost:27017
# It looks like you are trying to access mongodb over HTTP on the native driver port.

If the client doesn’t get this output, check whether the server’s firewall has the relevant port open.

2. Connecting to MongoDB from the Client

mongosh is MongoDB’s connection tool, but it isn’t very convenient in practice; pymongo, a Python implementation, is generally more convenient to work with.

2.1 Connecting to MongoDB with pymongo

Install pymongo on the client:

python3 -m pip install --user --upgrade pymongo

To connect to the client with pymongo, you need a client:

def get_client(host="", port="", account=None, password=None):
    login_kwargs = dict(host=host, port=port)
    if account:
        assert password is not None
        print("login with account and password")
        login_kwargs.update(dict(account=account, password=password))
    return MongoClient(**login_kwargs)

This gets you a client. Through this client, you can access MongoDB further. MongoDB has no password by default: on one hand, this is very friendly to beginners; on the other hand, it also creates a security risk, since it’s easy to forget to set a password.

Before going further, a bit of explanation of MongoDB is needed:

  • A single MongoDB service can hold multiple databases.
  • Each database in turn holds multiple collections.
  • Each collection stores many records, each stored as key-value pairs, similar to a Python dict.

If a database has no collections, that database doesn’t really exist either; if a collection has no data, that collection doesn’t really exist either. When you access a database or collection that doesn’t exist, it gets created automatically. In pymongo, you access them like this:

database = client['database_name']
collection = database['collection_name']
# collection.insert_one()

2.2 MongoDB’s various roles

MongoDB has thorough permission management, where different roles correspond to different permissions. If you’re unhappy with the existing roles’ permissions, you can also create your own role. MongoDB’s permissions include the following kinds:

  • read
  • readWrite

There are also several predefined roles:

  • userAdmin: manages users for one specific database (only manages users for that database, cannot modify other data)
  • userAdminAnyDatabase: can manage users for any database
  • dbAdmin: manages CRUD for one specific database, cannot manage users
  • dbAdminAnyDatabase: manages CRUD for all databases
  • root: superuser, has every permission

MongoDB’s permission management is based on the database — when configuring a role, you need to specify the database. User management and database management are handled separately. In fact, user management operates on MongoDB’s built-in database (named admin).

admindb = client.admin
admindb.command(
        "createUser",
        "dbadmin",
        pwd="dbadmin0",
        roles=[{"role": "dbAdminAnyDatabase", "db": "admin"}],
    )

After creating the user, you need to restart MongoDB on the server with the relevant flag added:

mongod --auth

3. Backing Up and Restoring MongoDB

For data safety, data needs to be backed up regularly. MongoDB also supports backing up and restoring an entire database, from either the server side or the client side. This section covers doing it on the server side.

3.1 Backing up on the server

Use mongodump to back up. Since MongoDB runs under Docker, you need to run it inside the container:

docker exec ${container_name} mongodump ${dump_dir}

Usually the backup ends up inside the container, so after it finishes, you need to copy it out of the container:

docker cp ${container_name}:${dump_dir} ${host_local_path}

This backs up every database and collection; you can add --database and --collection options to specify a particular database or collection instead.

3.2 Restoring on the server

First copy the backed-up folder into the container:

docker cp ${host_local_path} ${container_name}:${dump_dir}

Run mongorestore inside the container:

docker exec ${container_name} mongorestore ${dump_dir}

4. Summary

This article covered how to use MongoDB: running it on the server with Docker, connecting to and managing it with pymongo on the client, and also covered backing up and restoring MongoDB on the server side.