woman typing, probably working on a dope Flask back-end

The Task

Today we’re going to learn how to use PyMongo with MongoDB Atlas. PyMongo is a Python MongoDB database controller package. This package allows us to control any MongoDB database without the need for a Node.js back-end. It’s very simple to use and even integrate into Flask if you’d like to build a back-end written in Python. In this guide you’ll learn how to create a MongoDB Atlas database cluster as well control it with PyMongo!

Creating the Atlas Cluster

Let’s create our cluster in MongoDB Atlas so we can connect to a database with PyMongo. First, pop over to MongoDB Atlas to register for free. Follow the steps and create an account. Next, let’s select the free (shared) option in order to create our cluster:

MongoDB Atlas Sign-Up

From there, select all the default settings and click ‘create cluster’:

MongoDB Region Settings
Do try and choose the region that’s hosted closest to you

Wait for the M0 cluster to provision and then follow the quick start security questions to create cluster credentials. Additionally, add network access from your IP address or anywhere (typically you need to be selective with your allowed IPs for security reasons). Make sure to take note of your credentials.

Finally, simply create a new database with a collection from this menu:

Now, we’re ready to get started with PyMongo!

PyMongo Initialization

Initializing the connector is fairly simple. We just need to import the package and then create a variable for the cursor/controller. Here is the code:

import pymongo

client = pymongo.MongoClient("mongodb+srv://root:<password>@cluster0.ibe2h.mongodb.net/yourDatabaseName?retryWrites=true&w=majority")

The above code allows us to connect to our database using our cluster’s URI. Next let’s learn the write functionality of the package.

PyMongo with MongoDB Atlas – Write

Using key value notation we can select the database and collection we’d like to write a document to:

mydb = client["main"]
mycol = mydb["test"]

mydict = { "username": "picard", "password": "13jjb5jj5j50990" }


The variable mydb specifies our database while mycol assigns our collection name. Mydict is simply a JSON notation dictionary that acts as our document. Finally, the insert_one method writes our dictionary to the collection:

MongoDB PyMongo write results

PyMongo with MongoDB Atlas – Read

Reading data uses about the same syntax with a few different commands:

mydb = client["main"]
mycol = mydb["test"]

mydict = { "username": "picard", "password": "13jjb5jj5j50990" }


for x in mycol.find():

The find_one() method simply finds the first document in the collection. The find() method allows us to loop through the entire document pool within the collection. Here’s the results from our print statements:

MongoDB PyMongo read results

Now you know how to read and write collections using PyMongo with MongoDB Atlas. For more tutorials and on-demand help hire Code Dog Tutoring for fast help at: https://codedogtutoring.com/book-a-session/