Prerequisite: Python | Build a REST API using Flask
RESTful APIs are a very helpful tool to manage the backend of an application. It can be used to define the architecture as well as manage the dependencies of the application using routes managed by the API. Flask is a popular micro framework for building web applications.
In this article, we will learn how to make a Flask RESTful API that connects to the MongoDB Atlas database service by MongoDB. Before that, you have to make a user account on MongoDB Atlas using your email address.
Once you are done with the initial setup. Follow the steps below:
- Step1: Make your new project, by clicking on the ‘New Project’ button. Give your project a name and add members (if any) to how you want to give access to the project. Create a project by clicking on the ‘Create Project’ button. Your dashboard should look like this.
- Step 2: Click on ‘Build a cluster’. Select ‘Shared cluster’ from the pop-up. Select the required setting. For example : AWS Cloud provider and Mumbai region. You will be given 512MB space on the cloud for hosting your database. Create Cluster. Your dashboard will look like this.
- Step 3: Now, click on ‘Connect’. It will open a dialog box. Follow the instructions shown. Add your IP Address to the white list. This is the list of IP addresses that can access the database. Next, create your username and password for accessing the database. Now, click on ‘Choose a connection method’.
- Step 4: Now, click on ‘Connect your application’ on the new dialog box that appears. Select ‘Python’ in Driver dropdown and ‘3.6 or later’ in Version dropdown. Copy the link in the next line. It would look like:
- Step 5: Your username appears in the link (here, admin). Replace <password> with the password for this user.
This is the link or the URL which connects your Python API to the database. Now, create a sample database by clicking the ‘Collections’ tab on your dashboard and making a new database by clicking ‘Add My Own Data’. Give you a database name and a table name and finish creating a new database.
For this example, I have created a database named ‘Example’ and a table named ‘SampleTable’.
The below python code links to your MongoDB Atlas database using pymongo.
Note: Please install following libraries to run the flask API : flask, pymongo, dnspython, flask_cors
This will initiate a flask server on your http://localhost/5000 which will be connected to your MongoDB Atlas Database. On successfully execution, the console will look like:
Now you can create routes in the API and define functions to interact with the database. Given below is an example on how to map routes and use them to receive values and interact with the database.
- route(path, methods): is used to define the path through which you can call the corresponding functions. ‘<SomeData>’ is used to extract values from the path.
For example, for the insert function below, the path contains <name>, <id>, which means that the value in the path after the ‘insert-one/’ will be stored into name and the value after that will be stored in id. methods is used to define the type of methods the function will accept. You can read about from here.
- jsonify(object): is used to created a JSON object from the object passed.
- insert_one(object): is used to insert the passed object into the collection from which it is called.
- find_one(object): is used to find the object in the database which matches with the object passed as a key in the parameters. It returns whole document as an object. This method returns the first object that matches the passed key values.
- find(object): is used to find all the objects which matches the passed key values.
- update_one(object): is used to update the value of some object in the database which matches certain passed key values.
Output: The above code respond differently for different URL requests.
- insert-one request: http://127.0.0.1:5000/insert-one/chitrank/1/
This inserts the below object into the collection SampleTable and returns ‘Query Inserted…!!!’ after inserting the document.
style=”color:rgb(80,80,80);background-color:rgb(255,255,255);”> ‘Name’ : ‘chitrank’ ,
‘ID’ : ‘1’,
- find-one request: http://127.0.0.1:5000/find-one/Name/chitrank/
This request will return an object which matches the values passed in the URL. The returned object will be displayed on the browser window.
- find request: http://127.0.0.1:5000/find/
This request will return all the objects in the calling table. The returned objects will be displayed on the browser window.
- update-one request: http://127.0.0.1:5000/update/ID/1/Name/GeeksForGeeks/
This request will update the object which matches the first two parameter with the last two values. It will return ‘Updated Successfully’ on successful update
These are some basic operations which I have referred here. These are more than enough to manage a database. MongoDB provides a wide range of operations about which you can read fromMongoDB Official Documentation
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- 3D Plotting sample Data from MongoDB Atlas Using Python
- How to make a Google Translation API using Python?
- Oracle Database Connection in Python
- Inserting variables to database table using Python
- Python | Database management in PostgreSQL
- Connect MySQL database using MySQL-Connector Python
- Create a database in MongoDB using Python
- Create MySQL Database Login Page in Python using Tkinter
- Python - Database Manager (dbm) package
- PostgreSQL - Connect To PostgreSQL Database Server in Python
- PostgreSQL - Connecting to the database using Python
- CRUD Operations on Postgres using Async Database In Python
- Create Database in MariaDB using PyMySQL in Python
- Using Google Sheets as Database in Python
- Extract Data from Database using MySQL-Connector and XAMPP in Python
- Unicodedata – Unicode Database in Python
- Python MySQL - Create Database
- Python | PRAW - Python Reddit API Wrapper
- Different ways to access Instance Variable in Python
- Create and Access a Python Package
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.