Day 33: Master MongoDB Database Integration in Python

Archana PrustyArchana Prusty
3 min read

Introduction :

Welcome back to my Python journey! Yesterday, I laid the foundation with mysql database in python.

Today, I dove into something more on database MongoDB, Let's explore what I learned!

How to Install MongoDB on Windows:

Step 1)

Download MongoDB Community Server Go to link and Download MongoDB Community Server. We will install the 64-bit version for Windows.

Link :

Step 2) Click on Setup Once download is complete open the msi file. Click Next in the start up screen

Step 3) Accept the End-User License Agreement

Accept the End-User License Agreement

Click Next

Step 4) Click on the “complete” button

Step 5) Service Configuration

Select “Run service as Network Service user”. make a note of the data directory, we’ll need this later.

Click Next

Step 6) Start installation process

Click on the Install button to start the installation.

Step 7) Click on the Finish button


What is MongoDB database:

It is Document Database/NoSql database.

where we can use MongoDB:

We can use everywhere

For desktop applications

For mobile applications

For web applications, this database is more popular.

Relational Database vs Document Database:

There are 2 most common types of databases.

1. Relational Databases/SQl Databases

2. Document Databases/NoSQl Databases

  1. Relational Databases/SQl Databases

The data will be stored in tables and these tables has fixed schema.

Example :

Employee(eno,ename,esal,eaddr)

The data stored in tables has relationships like:

  1. one to one

  2. one to many

  3. many to one

etc

To retrieve data from relational databases, we have to write join queries which collects data from different tables.

Example :

Oracle,MySQL etc

2. Document Databases/NoSQl Databases:


Data will be stored in separate documents and each document is independent of others.

Example :

MongoDB, atlas,realm

MongoDB Structure:

  • MongoDB Physical database contains several logical databases.

  • Each database contains several collections. Collection is something like table in relational database.

  • Each collection contains several documents. Document is something like record/row in relational database.

  • A NoSQL database has a dynamic schema for unstructured data. In NoSQL, data is stored in several ways: it can be column-oriented, document-oriented, graph-based or organized as a key-value store.

A NoSQL database has the following advantages:

  • Documents can be created without having to first define their structure

  • Each document can have its own unique structure

  • The syntax can vary from database to database

  • Large volumes of structured, semi-structured, and unstructured data

  • Object-oriented programming that is easy to use and flexible

  • It is horizontally scalable

NoSQL Database Types:

  • Document databases pair each key with a complex data structure known as a document. A document is a set of key-value pairs.

Comparing MongoDB to RDBMS :

how to install mongo db : window 8

how to set path:

C:\Program Files\MongoDB\Server\4.0\bin ->copy this path after set path

How to start mongoserver :

open window cmd -> type mongod command

how to start mongo client:

open window cmd -> type mongo command

Query (CURD) →

add a new field :

db.collection_name.update({},{$set:{filedname:value}})

remove a field:

db.collection_name.update({}{$unset:{fileldname:value}})

remove a document from Collection:

db.collection_name.remove({}) -> Remove all document db.collection_name.remove({key:value}) -> remove single document

Limit() Method:

it accepts one number of argument, i.e the number of document want to be display.

Example :

db.COLLECTION_NAME.find().limit(2)

Challenges :

  1. Understanding python databases.

  2. Handling errors.

Resources :

  1. Official Python Documentation: python databases

  2. W3Schools' Python Tutorial: python databases

  3. Scaler's Python Courses : python databases

Goals for Tomorrow :

  1. Explore something more on mysql database and MongoDB.

Conclusion :

Day 32’s a success!

What are your favorite Python resources? Share in the comments below.

Connect with me :

GitHub: [ https://github.com/p-archana1 ]

LinkedIn : [ linkedin.com/in/archana-prusty-4aa0b827a ]

twitter : [ https://x.com/_archana77 ]

Join the conversation :

Share your own learning experiences or ask questions in the comments.

HAPPY LEARNING!!

THANK YOU!!

10
Subscribe to my newsletter

Read articles from Archana Prusty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Archana Prusty
Archana Prusty

I'm Archana, pursuing Graduation in Information technology and Management. I'm a fresher with expertise in Python programming. I'm excited to apply my skills in AI/ML learning , Python, Java and web development. Looking forward to collaborating and learning from industry experts.