Python 3 PyMongo Library Script to Connect Cloud Atlas NoSQL MongoDB Database and Insert Data Full Example For Beginners

Python 3 PyMongo Library Script to Connect Cloud Atlas NoSQL MongoDB Database and Insert Data Full Example For Beginners

 

Welcome folks today in this blog post we will be connecting to mongodb atlas database inside our python script using pymongo library. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install pymongo

 

pip install dnspython

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

# importing module 
from pymongo import MongoClient 

# creation of MongoClient 
client=MongoClient() 

# Connect with the portnumber and host 
client = MongoClient("mongodb+srv://root:helloworld@cluster0.jqlir.mongodb.net/first?retryWrites=true&w=majority") 

# Access database 
mydatabase = client["first"] 

# Access collection of the database 
mycollection=mydatabase["table"] 

# dictionary to be added in the database 
rec={ 
title: 'MongoDB and Python', 
description: 'MongoDB is no SQL database', 
tags: ['mongodb', 'database', 'NoSQL'], 
viewers: 104
} 

# inserting the data in the database 
rec = mydatabase.myTable.insert(record)

 

See also  Python 3 Script to Find GCD or HCF of two Numbers by Euclid’s Algorithm Using Recursion Full Project For Beginners

And now inside the above python script you need to replace the database name and server name and also the collection or table name that you will get from mongodb server as shown below

 

See also  Python 3 PyGame MineSweeper Game Clone Script GUI Desktop App Full Project For Beginners

 

 

Now if you execute the above python script by typing the below command as shown below

 

python app.py

 

 

 

So you can see one record has been added to the mongodb database because of the python script

 

Leave a Reply