Python MongoDB
🐍 Python MongoDB — Complete Guide
MongoDB is a NoSQL database that stores data in JSON-like documents.
Python can interact with MongoDB using the pymongo library.
1️⃣ Install PyMongo
2️⃣ Connect to MongoDB
Replace
localhost:27017if you have a remote MongoDB server.
3️⃣ Create a Collection
Collections are like tables in SQL.
4️⃣ Insert Documents
5️⃣ Read Documents
6️⃣ Update Documents
7️⃣ Delete Documents
8️⃣ Query Operators
| Operator | Description | Example |
|---|---|---|
$lt |
Less than | {"age": {"$lt": 25}} |
$gt |
Greater than | {"age": {"$gt": 20}} |
$lte |
Less than or equal | {"age": {"$lte": 25}} |
$gte |
Greater than or equal | {"age": {"$gte": 20}} |
$ne |
Not equal | {"name": {"$ne": "Vipul"}} |
$in |
In list | {"age": {"$in": [22,24]}} |
$nin |
Not in list | {"age": {"$nin": [22,24]}} |
9️⃣ Sorting and Limiting
10️⃣ Drop Collection or Database
11️⃣ Full Example: CRUD in MongoDB
✅ MongoDB Advantages in Python:
-
Schema-less, flexible data
-
Stores complex JSON-like documents
-
Fast for reads/writes
-
Easy integration with Python via
pymongo
