Python Tutorial 66 – Flask Application Database Using MongoDB Atlas and Python to Better Big Data Management

The non-SQL database is more advanced and capable of handling big data with dynamic schema features. In the era of Big Data, the non-SQL server seems to have a bright future, although in some cases, end-to-end business data solutions are still suitable to utilize SQL databases.

In this article, I will go through how to leverage MongoDB Altas integrating with Flask Applications using Python. It’s right for you to apply this approach to application development.

mongodb altas

The non-SQL database is more advanced and capable of handling big data with dynamic schema features. In the era of Big Data, the non-SQL server seems to have a bright future, although in some cases, end-to-end business data solutions are still suitable to utilize SQL databases.

In this article, I will go through how to leverage MongoDB Altas integrating with Flask Applications using Python. It’s right for you to apply this approach to application development.

Ingredients on Flask Application Database Using MongoDB Atlas & Python

Python3, MongoDB, Flask, Certifi

Table of Contents on Flask Application Database

MongoDB Intro and Free Atlas Account Creation

MongoDB is an open-source database that stores flexible JSON-like “documents,” which can have any number, name, or hierarchy of fields within, instead of rows of data as in a relational database. Python developers can think of MongoDB as a persistent, searchable repository of Python dictionaries (and, in fact, this is how PyMongo represents MongoDB documents).

MongoDB provides both free and paid service scope for developers to consider and adopt. No matter which one you are considering based on actual needs, I would say the offer is evenly friendly and reasonable, compared to avg. offerings in the market. Signing up for a free atlas account is super easy, which just requires your email address.

mongodb atlas

After having signed up for a free account, let’s create a new cluster. Basically, it means a new shared server with others. You can select a server by country, etc. For me, it’s almost the same in terms of a free service, although the loading speed should be faster if you select the closest to your target market.

mongodb atlas

Then, we can go to the connection section and copy and paste the server connection URL. Please be sure to remove pymongo from the given URL if we would import a mongo client from pymongo in the script. You might be able to learn more in a moment next paragraph.

client = MongoClient("mongodb+srv://adcabc:<password>@abcabc.tul2i.mongodb.net/?retryWrites=true&w=majority")

db = client.yourdatabasename

Create a Database & Collection

Then, we go to the browse collection section and create a new database. The free account database provides some samples for your test. In terms of new database creation, we just need to create two things. One is the new database name, and the other is the new collection name. Please be sure to copy these names. We will use them in a moment.

Import MongoDB a& Certifi Modules

There are two modules we need to import for MongoDB integration. Python 3.9 or above version has already included these modules, which are pymongo and ceritifi

from pymongo import MongoClient
import certifi

MongoDB Config Connection Code

client2 = MongoClient("Please copy and paste the cluster URL here with adding your preset password")
db2 = client2.easy2digitalTest
myCollection = db2.myEasy2Digital

For the password, all the special signal that existed in your password, such as #, $, ! and so on and so forth, needs to be converted into percent encodings, such as For more details, please google a free online converter and get your percent-encoded password.  

insert_one(), find({},{field_data:bool})

Okay, now it’s time to go through how to insert sign-up user data into the MongoDB database when the above deployments are ready in your flask application.

insert_one()

MongoDB uses a JSON-like data structure so that it’s super easy and friendly to insert or create a new dataset, such as handling the new user sign-up. Take this for example as well, this method can be applied to the user sign-up page that connects with MongoDB.

As we can see, we can create a block that includes any variable data set to shape a property. you can standardize the property or even customize the property based on actual membership diversification. On the whole, it’s super application-friendly.

name = "nerissa12324"
password = "153431234567891232"
email = "dddd@gmail.com"

newUser = {"Name": name, "Password": password, "Email": email}
newCreate = myCollection.insert_one(newUser)

The result is as follows:

find({},{field_data:bool})

Fetching only specific fields.  If you want to fetch only some fields then in the find method pass the first parameter as {} and the second parameter as 1 for the field that you want to fetch and 0 for those you don’t want to fetch.

readSpecific = myCollection.find({},{'_id':0, 'Name':1, 'Email':1})

for record in readSpecific:

if record['Email'] == "123@gmail.com":
print(record['Email'])

else:
pass

This method must be one of the most common and popular usages, notably in user matching, API development, targeting settings, etc

Full Python Script of Flask Application Database using MongoDB

If you are interested in the Full Python Script of Flask Application Database using MongoDB, please subscribe to our newsletter by adding the message “Python Tutorial 66”. We would send you the script immediately to your mailbox.

I hope you enjoy reading python Tutorial 66 – Flask Application Database Using MongoDB Atlas and Python to Better Big Data Management. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

Leave a Reply

Your email address will not be published. Required fields are marked *