How Do You Connect Python with MongoDB Atlas?

Connect Python with MongoDB Atlas

A previous guide here on the blog showed you how to connect Docker with MongoDB. It explained important steps including how to build a Docker network bridge, connect it with MongoDB, and binding containers. In this tutorial you will learn how to connect Python with MongoDB Atlas ?

Today, it’s all about connecting Python with MongoDB. Many developers like using this combination because the tools integrate really well. Python is great for handling JSON and BSON documents, and MongoDB uses both file types. Being a non-relational database, MongoDB doesn’t follow a predefined schema. You can also fetch data through dictionary-type syntax, making the operation relatively seamless. Objects can be easily manipulated as they’re compatible with dictionaries and lists when retrieved from MongoDB.

With that in mind, it’s time to proceed with the process.

Phase 1: Preparations and Prerequisites

Before anything else, note that it’s advisable to install and run a linter to maintain code quality. In another post, we’ve listed the top Python code linters for data scientists. Any of these tools can help you avoid bugs and errors, which is all the more vital for a dynamic language like Python.

For the back-end component, the cloud database MongoDB Atlas will be used. You can set up a free tier cluster and get 512MB storage to use for manipulating sample data.

The other required tool is the Python module, PyMongo. It acts as the bridge that lets Python communicate with MongoDB, letting you execute functions on your database.

Install PyMongo by opening a command window and typing “python -m pip install pymongo” (without quotes). Some users might get an error saying “ModuleNotFoundError: No module named ‘pymongo’”. To fix this, use the uninstall command “pip uninstall pymongo” and redo the installation.

You also need to install dnspython to enable the use of pymongo as a python MongoDB library. Use the command “python -m pip install dnspython“.

Phase 2: Building Your Database, Collection, and Inserting Documents

Next is creating your database. A guide by MongoDB notes that you must create a .py file using a text editor like Notepad. Use the script below to create a MongoDB client and be sure to follow correct indentation:

def get_database():

from pymongo import MongoClient

import pymongo

CONNECTION_STRING = "mongodb+srv://<username>:<password>@<cluster-name>.mongodb.net/myFirstDatabase"

from pymongo import MongoClient

client = MongoClient(CONNECTION_STRING)

return client['product_list']

if __name__ == "__main__":

dbname = get_database()

Change the values in pointy brackets into your own username, password, and cluster name, then provide the MongoDB atlas URL to connect python to MongoDB using pymongo. Connection_string is used for creating the MongoDB client and establishing your database connection. You can import MongoClient or use pymongo.MongoClient.

With this file, a database named product_list will be created. The if __name__ line is added to enable multiple files to reuse the function get_database().

Your database will exist only if you’ve added collections and documents, so the next step is to create your collection by adding the command “collection_name = dbname[collection_001]” to your .py file. You may change collection_001 into any name you like.

For documents, you may use the “insert_one” function for a single entry. To add multiple records simultaneously, a guide by Towards Data Science specifies using the “insert_many()” function. Take a look at the sample below for inserting two items:

item_1 = {

"_id" : "SKU20220101",

"item_name" : "Crocs",

"max_discount" : "5%",

"batch_number" : "AA20220101CRC",

"price" : 49,

"category" : "footwear"

}


item_2 = {

"_id" : "SKU20220202",

"item_name" : "Classic Tee",

"category" : "apparel",

"quantity" : 5,

"price" : 4.99,

"item_description" : "plain color shirt"

}
collection_name.insert_many([item_1,item_2])

To see your progress so far, go to your Atlas cluster through the MongoDB Atlas interface and check the collections tab. It’ll show the database and collection you made, along with all the data you specified in the inserted document above.

Phase 3: Querying and Indexing

The next part is to check your documents using the find() function. Another .py file must be created and executed via the command “python <insert>_<file>_<name>.py”. Use the script below:

from <your first .py file> import get_database

dbname = get_database()

collection_name = dbname[collection_001]

item_details = collection_name.find()

for item in item_details:

print(item)

Note that “print(item)” will show all the properties you added to every entry, which in this case are ID, item name, category, price, etc. This may not have good readability. To make it better, you may specify only the properties you like to see. For instance, using the command “print(item[‘item_name’], item[‘category’])” will only show item names and their respective categories.

If you have already added numerous documents and would like to view them in a more organized manner, you may use other PyMongo functions like “sort” and “limit”. A tutorial shared on the knowledge portal Analytics Vidhya explains that sort is for arranging your documents according to a preset filter like ID. Limit is for capping the number of retrieved documents using the find command.

For a very large collection such as at an enterprise level, creating indexes should be a priority. MongoDB has a built-in text-based search tool that allows you to scan through collections in the database. It’s akin to a sitemap index, only in this case it lists your database collections instead of individual sitemaps.

Note, however, that you can only have one text index for each collection. As stated in a post on Developer.com, you’ll get an error if you make another index for the same collection even if it’s for a different node. If other devs have access to the project, be sure everyone is informed to avoid redundancy.

Completing all the phases above should have given you a simple database that shows all the values you added. Now that you’ve successfully connect Python with MongoDB Atlas, you can begin to master the basic functions of PyMongo and eventually move on to advanced ones for building more complex applications.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Share via
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner