Connecting to MongoDB Atlas with Python-PyMongo

Soumi Bardhan
Analytics Vidhya
Published in
4 min readSep 26, 2021

In this article we will connect to a simple MongoDB database with python. We will perform simple operations on the data like :

  • Addition
  • Updation
  • Deletion
  • Adding multiple records at once

The video version of this article is available here.

MongoDB Atlas

Sign up for MongoDB Atlas here: Database Access | Atlas: MongoDB Atlas

Img source : Sign Up for MongoDB Atlas | Cloud MongoDB Hosting | MongoDB

Select a deployment option :

Img Source : Sign Up for MongoDB Atlas | Cloud MongoDB Hosting | MongoDB

Select the desired cloud provider and the closest region :

Img by author

In your database access tab, go to “Add a New Database User” :

Img by author

Create a cluster to test :

Img by author

You can either load an existing sample dataset or create and add your own data. Click on the “Add My Own Data” button :

Img by author

This dialog box will open where you must enter a new Database name and collection name for your new database :

Img by author

You will be able to see this screen :

Img by author

Now go to Network Access to connect your cluster. Add your IP address here as your Database User has already been created :

Img by author

Once that is done, you can choose a connection method. Click on “Connect your application” :

Img by author

Select your driver and version as “Python and 3.6” or later respectively :

Img by author

You will get a string similar to this :

mongodb+srv://SoumiBardhan:<password>@cluster-test.fndbj.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

Copy it and save it somewhere. Now you can connect to your database. Start by creating a new python file on your system test_mongo_connection.py. Import pymongo which is the python driver which lets us connect to a MongoDB database.

import pymongo
from pymongo import MongoClient

Connect the cluster using the URL. Replace your username and password in this URL.

cluster = MongoClient("mongodb+srv://<USERNAME>:<PASSWORD>@cluster-test.fndbj.mongodb.net/UserData?retryWrites=true&w=majority")

Create a new database db by providing the name of the database you created earlier. Initialise the collection by providing the collection name you created.

db = cluster["UserData"]collection = db["Flask_mongo"]

Now lets see how we can insert some data. You can do that by adding this statement in the python file :

collection.insert_one({"_id":0, "user_name":"Soumi"})
collection.insert_one({"_id":100, "user_name":"Ravi"})

You can also add more fields. Next, execute the file with python3 test_mongo_connection.py.

You will be able to see the data updated on the dashboard.

Img by author

Now, lets see how we can delete posts :

collection.delete_one({"_id":0, "user_name":"Soumi"})

This post will be deleted, and you will be able to view that from the database.

You can also add mutiple posts together like this :

post1 = {"_id":"0", "user_name":"Soumi"}post2 = {"_id":"100", "user_name":"Ravi"}collection.insert_many([post1,post2])

Update a post with a certain ID :

collection.find_one_and_update({"_id":"0"}, {"$set" : {"user_name" : updated_user_name}}, upsert = 0 )

So, based on requirements, you may need to whitelist your current IP as required as it changes with time.

Img by author

In this article, we connected to a MongoDB database and performed some basic operations. In the next article, we make a simple User login system using Flask and MongoDB.

--

--