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
- 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 https://www.mongodb.com/cloud/atlas 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 0.0.0.0/0 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
['knforg', 'admin', 'local']
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
Using collection.insert_many(data) we can able add multiple documents in collection.
Write code in Python to retrieve records:
Similar Topic - Python-Create a CRUD Restful Service API using FLASK and MYSQL