Skip to main content

Python with MongoDB Atlas - CRUD

In this article, we will learn how to interact with Python and MongoDB.And we will learn to create, read, update, and delete data from the MongoDB Atlas using python
Technologies used:
  • Python 3
  • Create an Account on MongoDB Atlas Website
  • pymongo[srv] library
Please go through the following steps in order to learn how to interact between Python and MongoDB:

Step 1.Create an Account on MongoDB Atlas
Create an account on the MongoDB Atlas website if you do not have one already. Go to and click Try Free green button on the top right corner to create an account.

Step 2 — Create a Starter Cluster
After you log in to your MongoDB Atlas account, you can create a starter cluster by selecting your desired cloud service provider, cluster tier, and your cluster name. It takes 1–3 mins for the cluster to be ready. When the cluster is ready, it looks like something below:
Step 3 — Click on the connect button, a popup window will show up

From this window, you can add your IP address to the configuration so that your computer can access the database. Or you can add IP address to the configuration so that any IP address can access the database.

You will also need to create a MongoDB user by typing your user name and password.

Step 4.Choose a connection method

You'll get a window with different connection options; click the "Connect your application" option.

Step 5.Copy connection String

Choose Python as Driver and choose the right version of the Python you are using and you will see the connection string.

Step 6.Connecting to the cluster from a Python using below script
import pymongo
client = pymongo.MongoClient("<the atlas connection string>")
..replacing <the atlas connection string> with the actual connection string we got from the Atlas site earlier, with <password> replaced with the password you used when setting up the user.
['knforg', 'admin', 'local']
CRUD Operations
As we know in mongodb provides our database at the top hierarchy. In the database, we store collections and inside the collections, we add documents.
Create a document
import pymongo  
client = pymongo.MongoClient("<the atlas connection string>")
collection = client.knforg.employee  
data = [  
         "uname": "Sibin",  
         "email": "",  
         "contact": "8075866854"  
         "uname": "Safin",  
         "email": "",  
         "contact": "8075866854"  
print('Users Inserted Successfully')
Using collection.insert_many(data) we can able add multiple documents in collection. 
Retrive records:
Write code in Python to retrieve records:
import pymongo  
client = pymongo.MongoClient("<the atlas connection string>")
collection = client.knforg.employee
print('Find One document')  
print('Find all documents')  
for x in collection.find():  
print('Find documents with condition')  
for x in collection.find({"uname": "Safin"}):  

Update document:
import pymongo  
client = pymongo.MongoClient("<the atlas connection string>")
collection = client.knforg.employee
myquery = {"uname": "Safin"}  
newvalues = {"$set": {"uname": "Sibin-123"}}   
collection.update_one(myquery, newvalues)  
print('Employee Updated Successfully') 
Delete document:
import pymongo  
client = pymongo.MongoClient("<the atlas connection string>")
collection = client.knforg.employee
myquery = {"uname": "Sibin"}  
print('Employee Deleted Successfully') 

Similar Topic - Python-Create a CRUD Restful Service API using FLASK and MYSQL


Popular posts from this blog

Spring boot video streaming example-HTML5

Hello everyone, Today we will learn how to stream MP4 video using Spring. You can download the source code of this sample application from our GitHub repository. Download Link is provided at the end of this post.
User Interface

Project Structure

Home Controller
public class HomeController {
 private MyResourceHttpRequestHandler handler;
 private final static File MP4_FILE = new File("D:\\videofiles\\video1.mp4");
 // supports byte-range requests
 public String home() {
  return "index";
 // supports byte-range requests
 public void byterange( HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setAttribute(MyResourceHttpRequestHandler.ATTR_FILE, MP4_FILE);
  handler.handleRequest(request, response);
Spring Boot 
@ComponentScan({ "com" })
public class KnowledgefactorydemoApplication {
 public static vo…

DataTable-Pagination example with Spring boot,jQuery and ajax free download

This article shows you DataTable-Pagination example with Spring boot, jQuery and ajax. 

Following technologies stack being used:
Spring Boot 2.1.1.RELEASESpring 5.1.3.RELEASE Maven 3DatatablejQueryFree Markerjavascriptajax Project Structure
View(index.ftl) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Knf|Pagination-Datatables</title> <!-- Tell the browser to be responsive to screen width --> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="../../css/jquery.dataTables.min.css"> <body>  <section class="content">   <div class="container-fluid">    <table id="example" class="display" style="width: 100%">     <thead>      <tr>       <th>First name</th>       <th>La…

9 Best Job Posting Sites for Employee 2020-2022

One of the most common ways today’s job seekers uncover employment opportunities are by using online sources. There are hundreds of job boards, both generic and niche, as well as aggregators, social media channels, networking groups, and staffing company websites to choose from. The good news is they provide plenty of listings. The bad news is you can be easily overwhelmed by so many options. Here, knowledge factory offers 9 of what is generally considered the best job search websites around. There are many others, of course, but this list of best job search websites should give you a good starting point for your next job hunt: 1. LinkedIn
This top networking site allows you to find jobs through your extended network. User profiles serve as resumes. You can easily share career-related content and solicit or supply recommendations. Additionally, you can join groups, participate in conversations, and follow companies you find interesting and relevant to your job search. Premium pai…