![]()
MongoDB is a powerful NoSQL database that stores data in flexible, JSON-like documents. It is widely used for applications requiring high scalability, real-time analytics, and unstructured data handling. Python provides seamless integration with MongoDB using the pymongo library.
1. Why Use MongoDB with Python?
Schema-less – No predefined schema required.
JSON-like Documents – Stores data as BSON (Binary JSON).
Scalability – Supports horizontal scaling (sharding).
High Performance – Optimized for read and write operations.
Flexible Queries – Supports powerful querying and indexing.
2. Installing MongoDB and PyMongo
Step 1: Install MongoDB
- Download MongoDB from MongoDB Official Website.
- Install and Start MongoDB Service:
- Windows: Run
mongod.exe - Linux/macOS: Use
brew services start mongodb-community@6.0
- Windows: Run
- Verify MongoDB is running using: bashCopyEdit
mongo --version
Step 2: Install PyMongo
To interact with MongoDB using Python, install pymongo:
pip install pymongo
3. Connecting Python to MongoDB
Establish a Connection
from pymongo import MongoClient
# Connect to MongoDB server
client = MongoClient("mongodb://localhost:27017/")
# Create or access a database
db = client["testdb"]
print("Connected to MongoDB successfully!")
MongoClient("mongodb://localhost:27017/") connects to the local MongoDB instance.
db = client["testdb"] creates (if not exists) or accesses a database named testdb.
4. Creating and Accessing Collections
Create a Collection
collection = db["users"]
print("Collection created!")
Collections in MongoDB are similar to tables in relational databases.
List All Collections in the Database
print(db.list_collection_names())
5. Inserting Data into MongoDB
Insert a Single Document
user = {"name": "John Doe", "age": 25, "email": "john@example.com"}
collection.insert_one(user)
print("User inserted successfully!")
_id is automatically generated for each document.
Insert Multiple Documents
users = [
{"name": "Alice", "age": 30, "email": "alice@example.com"},
{"name": "Bob", "age": 22, "email": "bob@example.com"},
{"name": "Charlie", "age": 28, "email": "charlie@example.com"}
]
collection.insert_many(users)
print("Multiple users inserted successfully!")
insert_many() allows batch insertion.
6. Retrieving Data from MongoDB
Fetch All Documents
for user in collection.find():
print(user)
Returns all documents in the collection.
Retrieve a Specific Document
user = collection.find_one({"name": "Alice"})
print(user)
Fetches the first document that matches the query.
Retrieve Specific Fields
for user in collection.find({}, {"name": 1, "age": 1, "_id": 0}):
print(user)
_id: 0 excludes _id from results.
Filter Data with Conditions
for user in collection.find({"age": {"$gt": 25}}): # Users older than 25
print(user)
MongoDB supports powerful query operators ($gt, $lt, $in, $regex, etc.).
7. Updating Data in MongoDB
Update a Single Document
collection.update_one({"name": "Alice"}, {"$set": {"age": 35}})
print("User updated successfully!")
update_one() modifies the first matching document.
Update Multiple Documents
collection.update_many({"age": {"$lt": 30}}, {"$set": {"status": "young"}})
print("Users updated successfully!")
update_many() updates all matching documents.
8. Deleting Data in MongoDB
Delete a Single Document
collection.delete_one({"name": "Bob"})
print("User deleted successfully!")
Deletes the first matching document.
Delete Multiple Documents
collection.delete_many({"age": {"$gt": 50}})
print("Users deleted successfully!")
Deletes all users older than 50.
9. Indexing for Performance Optimization
Indexes improve query performance.
Create an Index on the email Field
collection.create_index([("email", 1)])
print("Index created on email field!")
Use indexes for faster searching and sorting.
10. Aggregation in MongoDB
Aggregation is used for data analysis.
Example: Count Users by Age
pipeline = [
{"$group": {"_id": "$age", "count": {"$sum": 1}}}
]
results = collection.aggregate(pipeline)
for result in results:
print(result)
Groups users by age and counts occurrences.
11. Exporting and Importing Data
Export Data to JSON
import json
data = list(collection.find({}, {"_id": 0})) # Exclude _id for readability
with open("users.json", "w") as file:
json.dump(data, file, indent=4)
print("Data exported successfully!")
Import Data from JSON
with open("users.json", "r") as file:
users = json.load(file)
collection.insert_many(users)
print("Data imported successfully!")
Useful for data migration and backups.
12. Closing MongoDB Connection
client.close()
print("MongoDB connection closed.")
Always close the connection to free resources.
13. Advanced MongoDB Features in Python
| Feature | Description |
|---|---|
| Sharding | Distributes data across multiple servers for scalability |
| Replication | Ensures high availability of data |
| GridFS | Stores large files (images, videos) in MongoDB |
| Full-Text Search | Enables efficient text searches |
| Geospatial Queries | Handles location-based queries |
