Skip to main content

Command Palette

Search for a command to run...

MongoDB Made Simple: A Basic Guide to get started

How to set up MongoDB for a Python Project

Published
5 min read
MongoDB Made Simple: A Basic Guide to get started
M

I’m Prunella DOUSSO, an IT student with a passion for data science and software engineering. This blog is my space to share insights, experiences, and opinions, making tech discoveries fun and accessible to everyone, no matter your skill level.

Hello tribe! I hope you are all doing wonderful!

Wou! First really tech-related article!

Whether you are into software engineering or data science, you're likely familiar with different types of databases. There are SQL-based databases like MySQL, BigQuery and SQLite, and NoSQL databases such as MongoDB, Apache Cassandra, and Redis.

NoSQL databases are widely recognised for their ease of development, functionality, and performance at scale. Knowing how to work with at least one of these technologies is crucial for students, professionals, and beginners alike.

In this article, we’ll explore MongoDB—a widely-used NoSQL database. It is actually sort of a prequel, a foundational piece for an upcoming post, where I’ll walk you through a small project I built using MongoDB.

But before diving in, let’s first cover the basics of MongoDB and how to integrate it into a project. Good reading =) !

MongoDB Atlas is a fully managed cloud database service that simplifies deploying, managing, and scaling MongoDB databases. This guide will walk you through setting up your MongoDB Atlas account, configuring API keys, and connecting to MongoDB from Python.

1. Installing MongoDB Atlas CLI

To interact with MongoDB Atlas via the command line, you’ll need to install the MongoDB Atlas CLI. Follow the instructions in the official MongoDB Atlas CLI installation guide. Personally, I used the binary download method, as I find it easier.

2. First-Time Setup with MongoDB Atlas

When you sign up for MongoDB Atlas for the first time, you will automatically be provided with a free tier cluster. This cluster is sufficient for small-scale projects and testing, so no need to create one right away.

But if you need to create a new cluster, you can follow the instructions here.

MongoDB Atlas will provide you with key information such as your connection string, your database username and password, which you'll need later to connect your Python application to MongoDB. Be sure to save this connection string in a secure file or use environment variables to store it. (we’ll cover it later).

3. Connecting to MongoDB Atlas via the CLI

To interact with MongoDB Atlas from the command line, you’ll need to authenticate using one of two commands:

- atlas auth login: to log in interactively via your browser and authenticate with your Atlas login credentials and a one-time authentication token.

- atlas config init: this is the command we’ll use, as it’s tailored for programmatic use. It doesn't require manual login or token verification. But to authenticate using this command, we must configure API Keys.

Why Use API Keys?

API keys allow programmatic access to MongoDB Atlas without needing to log in interactively every time. They are especially useful for automation, scripts, and integrating with your development environment.

4. Configuring API Keys for the Atlas CLI

To configure API keys, follow these steps:

a. Log in to MongoDB Atlas

Go to the MongoDB Atlas Official Website and log in to your account.

b. Navigate to the Project Settings

Once logged in, select the project you want to work with from the Projects dropdown in the top-left corner. Then, go to Project Settings by clicking the settings icon (⚙️) on the left sidebar.

c. Create an API Key

- Click on "Access Manager" under the Security tab on the left sidebar.

- In the Access Manager page, click on the API Keys tab.

- Click Create API Key.

d. Configure API Key Details

- Description: Enter a description for your API key (e.g., “Atlas CLI Access”).

- Role: Choose a role, like Project Owner or Organization Owner, to give the key necessary permissions. I chose to choose everything, as I am also learning and wanted to make sure I had ALL access to everything (don’t be like me! Only give the necessary permissions ;) )

- Add your IP address to ensure secure access.

- Copy the Keys: Once the keys are generated, copy the Public and Private Keys somewhere secure because they won’t be shown again.

e. Configure the API Key in the Atlas CLI

Open your terminal and run the following command:

atlas config init

When prompted, paste the Public Key and Private Key from the previous step. Accept the default profile options (Org ID, Project ID, etc.) unless you need to specify custom values.

You can now use the API keys to perform various operations such as listing clusters, managing users, or viewing logs. Here’s an example command:

atlas clusters list --projectId <Your_Project_ID>.

For a full list of Atlas CLI commands, you can run atlas –-help. You can know more about Atlas Administration API here and for more complex operations, refer to this.

5. Connecting MongoDB Atlas to Your Python Project

Well, here, the most tricky part is done. MongoDB has provided a really good guide on how to connect Python and MongoDB Atlas with some prerequisites about Python and MongoDB. It goes over the creation of a database and a collection in Python, document insertions, querying and indexing in Python. This guide will help you get started pretty easily on the use of MongoDB in python and acquire the basics for our To-Do List Manager.

Before we close out this article, I do want to talk about securing your connection strings and atlas key information. For security purposes, it’s recommended to store those sensitive information in environment variables. Here’s how to do that:

- On Linux/macOS:

export MONGO_URI="your_connection_string"

- In your Python code, access the environment variable like this:

import os

from pymongo import MongoClient

CONNECTION_STRING = os.getenv("MONGO_URI")

client = MongoClient(CONNECTION_STRING)

And here it is! All done!

With this guide, you’ve set up MongoDB Atlas, configured API keys for secure access, and connected it to your Python project. You’re now ready to start developing with MongoDB in your Python application! For further reading about MongoDB, you can later, explore its extensive documentation and learn more about cluster management and database operations.

Hope you enjoyed it, see you in the next post!