MongoDB and Python
MongoDB and Python
1. AIM
To study MongoDB, its features, architecture, and learn how to use Python (PyMongo library) to build applications that connect to MongoDB and perform CRUD operations.
2. INTRODUCTION
What is MongoDB?
MongoDB is a NoSQL document-oriented database, meaning it stores data in JSON-like documents instead of traditional rows and columns. It provides:
-
Flexible schema
-
High speed and scalability
-
Easy handling of big data
-
Horizontal scaling using sharding
MongoDB stores documents in BSON format (Binary JSON).
3. FEATURES OF MONGODB
1. Schema-less Database
Collections do not enforce any predefined structure. Each document can be different.
2. Document-Oriented
Data is stored as key–value pairs, similar to JSON.
3. High Performance
Provides fast reads/writes due to indexing, in-memory computing, and flexible storage.
4. Horizontal Scalability
Uses sharding to distribute data across multiple servers.
5. Replication
Provides failover and data redundancy using replica sets.
6. Rich Query Language
Supports filters, projections, indexing, aggregations, text search, geospatial queries, etc.
4. MONGODB ARCHITECTURE
Database → Collection → Documents
Example:
-
Database: college
-
Collection: students
-
Documents:
MongoDB automatically adds an _id field as a unique primary key unless explicitly provided.
5. INSTALLATION OF MONGODB (Ubuntu/Linux)
To check service:
6. MONGODB BASIC COMMANDS (SHELL)
Create or switch database:
Create collection:
Insert document:
Find:
Update:
Delete:
7. USING PYTHON WITH MONGODB (PyMongo)
Python connects to MongoDB through the pymongo library.
Install PyMongo
8. PYTHON PROGRAM TO CONNECT TO MONGODB
9. PYTHON CRUD OPERATIONS
A. INSERT DOCUMENTS
Insert Many:
B. READ DOCUMENTS
Fetch all documents:
Fetch with filter:
C. UPDATE DOCUMENTS
Update one:
Update many:
D. DELETE DOCUMENTS
10. PYTHON APPLICATION EXAMPLE (STUDENT MANAGEMENT SYSTEM)
Menu-Driven Program
This program shows Python + MongoDB in a real-world CRUD application.
11. ADVANCED PYTHON + MONGODB FEATURES
1. Aggregation
2. Indexing
3. Sorting
4. Using ObjectId
OBSERVATION
-
MongoDB supports flexible schema and dynamic documents.
-
Python PyMongo library provides easy CRUD operations.
-
Documents can contain nested structures, arrays, maps, etc.
-
Aggregation and indexing improve query efficiency.
RESULT
MongoDB concepts were studied and advanced CRUD operations were implemented using Python and PyMongo to build a student management application.
Comments
Post a Comment