MongoDB isn't just about storing data — it's about efficiently querying, analyzing, and managing it in real-world applications. Whether you're building an e-commerce platform, analytics dashboard, or content management system, mastering daily MongoDB operations is crucial.
This 3-day intensive guide is designed to take you from basic queries to advanced real-world scenarios that you'll encounter every day as a developer.
Getting Started – Sample Data Setup
Before we dive in, let's set up a realistic database that mirrors real-world applications. We'll create an e-commerce database with users, products, and orders.
📁 users.json
[
{ "_id": 1, "firstName": "Sadat Shahriar", "lastName": "Rafin", "email": "rafin@example.com", "age": 24, "city": "Dhaka", "country": "Bangladesh", "role": "admin", "joinDate": { "$date": "2024-01-15T00:00:00Z" } },
{ "_id": 2, "firstName": "MD. EASIN", "lastName": "AHMED", "email": "easin@example.com", "age": 27, "city": "Chittagong", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-02-20T00:00:00Z" } },
{ "_id": 3, "firstName": "MD RANJU", "lastName": "HASAN", "email": "ranju@example.com", "age": 30, "city": "Dhaka", "country": "Bangladesh", "role": "moderator", "joinDate": { "$date": "2024-03-10T00:00:00Z" } },
{ "_id": 4, "firstName": "MD. Rakibul Islam", "lastName": "Nayon", "email": "nayon@example.com", "age": 22, "city": "Sylhet", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-04-05T00:00:00Z" } },
{ "_id": 5, "firstName": "Md.", "lastName": "Redwan", "email": "redwan@example.com", "age": 35, "city": "Rajshahi", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-01-25T00:00:00Z" } },
{ "_id": 6, "firstName": "Rezwana", "lastName": "Sharmin", "email": "rezwana@example.com", "age": 28, "city": "Khulna", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-05-15T00:00:00Z" } },
{ "_id": 7, "firstName": "Golam Kibria", "lastName": "Hassan", "email": "kibria@example.com", "age": 26, "city": "Dhaka", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-06-10T00:00:00Z" } },
{ "_id": 8, "firstName": "Sumon", "lastName": "Mia", "email": "sumon@example.com", "age": 29, "city": "Barishal", "country": "Bangladesh", "role": "moderator", "joinDate": { "$date": "2024-03-20T00:00:00Z" } },
{ "_id": 9, "firstName": "MD. JOBAYER HOSSAIN", "lastName": "CHOWDHURY", "email": "jobayer@example.com", "age": 31, "city": "Comilla", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-02-15T00:00:00Z" } },
{ "_id": 10, "firstName": "Eahea Bin", "lastName": "Ayub", "email": "ayub@example.com", "age": 25, "city": "Dhaka", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-07-01T00:00:00Z" } },
{ "_id": 11, "firstName": "Md. Abtahi", "lastName": "Abid", "email": "abid@example.com", "age": 23, "city": "Mymensingh", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-04-22T00:00:00Z" } },
{ "_id": 12, "firstName": "Khandaker Rafidul", "lastName": "Haque", "email": "rafidul@example.com", "age": 33, "city": "Sylhet", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-01-10T00:00:00Z" } },
{ "_id": 13, "firstName": "Md. Nuruz Jaman", "lastName": "Nishat", "email": "nishat@example.com", "age": 27, "city": "Rangpur", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-05-05T00:00:00Z" } },
{ "_id": 14, "firstName": "Robiul", "lastName": "Hasan", "email": "robiul@example.com", "age": 28, "city": "Dhaka", "country": "Bangladesh", "role": "user", "joinDate": { "$date": "2024-03-30T00:00:00Z" } },
{ "_id": 15, "firstName": "Md. Atiqur", "lastName": "Rahman", "email": "atiqur@example.com", "age": 32, "city": "Chittagong", "country": "Bangladesh", "role": "moderator", "joinDate": { "$date": "2024-02-28T00:00:00Z" } }
]
📁 products.json
[
{ "_id": 101, "name": "Laptop Pro 15", "category": "Electronics", "price": 85000, "stock": 15, "tags": ["tech", "office", "premium"], "rating": 4.5 },
{ "_id": 102, "name": "Smartphone X", "category": "Electronics", "price": 45000, "stock": 30, "tags": ["tech", "mobile"], "rating": 4.2 },
{ "_id": 103, "name": "Coffee Maker Deluxe", "category": "Home", "price": 3500, "stock": 20, "tags": ["home", "kitchen"], "rating": 4.0 },
{ "_id": 104, "name": "Gaming Chair RGB", "category": "Furniture", "price": 12000, "stock": 8, "tags": ["tech", "furniture", "gaming"], "rating": 4.7 },
{ "_id": 105, "name": "Wireless Headphones", "category": "Electronics", "price": 2500, "stock": 50, "tags": ["tech", "music"], "rating": 4.3 },
{ "_id": 106, "name": "Smart Watch Pro", "category": "Electronics", "price": 15000, "stock": 25, "tags": ["tech", "wearable"], "rating": 4.6 },
{ "_id": 107, "name": "Desk Lamp LED", "category": "Home", "price": 1500, "stock": 40, "tags": ["home", "office"], "rating": 4.1 },
{ "_id": 108, "name": "Mechanical Keyboard", "category": "Electronics", "price": 5000, "stock": 18, "tags": ["tech", "gaming"], "rating": 4.4 },
{ "_id": 109, "name": "Office Desk", "category": "Furniture", "price": 18000, "stock": 12, "tags": ["furniture", "office"], "rating": 4.5 },
{ "_id": 110, "name": "Bluetooth Speaker", "category": "Electronics", "price": 3000, "stock": 35, "tags": ["tech", "music"], "rating": 4.2 }
]
📁 orders.json
[
{ "_id": 201, "userId": 1, "items": [{"productId": 101, "qty": 1, "price": 85000}], "total": 85000, "status": "delivered", "orderDate": { "$date": "2025-01-10T00:00:00Z" } },
{ "_id": 202, "userId": 2, "items": [{"productId": 103, "qty": 2, "price": 3500}], "total": 7000, "status": "pending", "orderDate": { "$date": "2025-01-15T00:00:00Z" } },
{ "_id": 203, "userId": 3, "items": [{"productId": 102, "qty": 1, "price": 45000}, {"productId": 105, "qty": 1, "price": 2500}], "total": 47500, "status": "shipped", "orderDate": { "$date": "2025-01-12T00:00:00Z" } },
{ "_id": 204, "userId": 1, "items": [{"productId": 104, "qty": 1, "price": 12000}], "total": 12000, "status": "delivered", "orderDate": { "$date": "2025-01-08T00:00:00Z" } },
{ "_id": 205, "userId": 4, "items": [{"productId": 105, "qty": 3, "price": 2500}], "total": 7500, "status": "processing", "orderDate": { "$date": "2025-01-18T00:00:00Z" } },
{ "_id": 206, "userId": 5, "items": [{"productId": 106, "qty": 1, "price": 15000}], "total": 15000, "status": "delivered", "orderDate": { "$date": "2025-01-05T00:00:00Z" } },
{ "_id": 207, "userId": 6, "items": [{"productId": 107, "qty": 2, "price": 1500}], "total": 3000, "status": "shipped", "orderDate": { "$date": "2025-01-20T00:00:00Z" } },
{ "_id": 208, "userId": 7, "items": [{"productId": 108, "qty": 1, "price": 5000}], "total": 5000, "status": "pending", "orderDate": { "$date": "2025-01-22T00:00:00Z" } },
{ "_id": 209, "userId": 8, "items": [{"productId": 109, "qty": 1, "price": 18000}], "total": 18000, "status": "delivered", "orderDate": { "$date": "2025-01-07T00:00:00Z" } },
{ "_id": 210, "userId": 9, "items": [{"productId": 110, "qty": 2, "price": 3000}], "total": 6000, "status": "shipped", "orderDate": { "$date": "2025-01-14T00:00:00Z" } },
{ "_id": 211, "userId": 10, "items": [{"productId": 102, "qty": 1, "price": 45000}], "total": 45000, "status": "processing", "orderDate": { "$date": "2025-01-19T00:00:00Z" } },
{ "_id": 212, "userId": 11, "items": [{"productId": 105, "qty": 1, "price": 2500}], "total": 2500, "status": "delivered", "orderDate": { "$date": "2025-01-11T00:00:00Z" } },
{ "_id": 213, "userId": 12, "items": [{"productId": 106, "qty": 1, "price": 15000}, {"productId": 108, "qty": 1, "price": 5000}], "total": 20000, "status": "shipped", "orderDate": { "$date": "2025-01-16T00:00:00Z" } },
{ "_id": 214, "userId": 13, "items": [{"productId": 103, "qty": 1, "price": 3500}], "total": 3500, "status": "pending", "orderDate": { "$date": "2025-01-21T00:00:00Z" } },
{ "_id": 215, "userId": 14, "items": [{"productId": 101, "qty": 1, "price": 85000}], "total": 85000, "status": "processing", "orderDate": { "$date": "2025-01-17T00:00:00Z" } }
]
How to Import Data
Using MongoDB Shell:
mongoimport --db ecommerce --collection users --file users.json --jsonArray
mongoimport --db ecommerce --collection products --file products.json --jsonArray
mongoimport --db ecommerce --collection orders --file orders.json --jsonArray
Using MongoDB Compass:
- Create a new database called ecommerce
- Create collections: users, products, orders
- Import JSON files into respective collections
Verify Import:
use ecommerce
db.users.countDocuments() // Should return 15
db.products.countDocuments() // Should return 10
db.orders.countDocuments() // Should return 15
Top comments (0)