1. MongoDB là gì?
MongoDB là cơ sở dữ liệu NoSQL dạng document, dùng JSON/BSON để lưu trữ dữ liệu, không cần schema cố định, phù hợp với:

- Web app, mobile app, SaaS
- Dữ liệu phi cấu trúc hoặc bán cấu trúc
- Realtime app, big data analytics
Ưu điểm: linh hoạt schema, scale dễ dàng, query nhanh, hỗ trợ indexing, aggregation, replication, sharding.
2. Cài đặt MongoDB cho người mới
- Tải MongoDB Community Server: https://www.mongodb.com/try/download/community
- Cài đặt MongoDB Compass (GUI) hoặc dùng CLI mongo shell
- Tạo database demo:
bashuse myFirstDB
Tạo collection:
db.createCollection("users")
3. CRUD Trong MongoDB
a) Create - Thêm dữ liệu
db.users.insertOne({name: "Alice", email: "alice@mail.com", created_at: new Date()});
db.users.insertMany([
{name: "Bob", email: "bob@mail.com"},
{name: "Charlie", email: "charlie@mail.com"}
]);
b) Read - Truy vấn dữ liệu
db.users.find(); // tất cả dữ liệu
db.users.find({name: "Alice"});
db.users.find().sort({created_at: -1}).limit(5);
c) Update - Cập nhật dữ liệu
db.users.updateOne({name: "Alice"}, {$set: {status: "active"}});
db.users.updateMany({status: null}, {$set: {status: "inactive"}});
d) Delete - Xóa dữ liệu
db.users.deleteOne({name: "Charlie"});
db.users.deleteMany({status: "inactive"});
4. Aggregation Trong MongoDB
Aggregation Pipeline: xử lý, lọc, nhóm dữ liệu
db.orders.aggregate([
{$match: {status: "paid"}},
{$group: {_id: "$user_id", totalAmount: {$sum: "$amount"}}},
{$sort: {totalAmount: -1}}
]);
Các stage thường dùng: $match, $group, $project, $sort, $limit, $lookup
5. Index Trong MongoDB
Tăng tốc truy vấn, giảm scan toàn bộ collection
Ví dụ:
db.users.createIndex({email: 1}); // ascending
db.users.createIndex({name: 1, status: 1}); // compound index
Kiểm tra index:
db.users.getIndexes();
6. Mini Project & Portfolio
Mini project: blog, e-commerce, user management
Backend: Node.js + Express, Django, Flask
CRUD, aggregation, index, query optimization
Deploy demo online: Heroku, Vercel, Netlify
Portfolio: GitHub repo, README chi tiết, demo live
Freelance: áp dụng MongoDB cho dự án client
7. Tips Thực Hành MongoDB
Thực hành CRUD & aggregation -> nắm query cơ bản
Index & optimize query -> cải thiện performance
Mini project & deploy demo online -> portfolio
Tham gia cộng đồng & mentor: StackOverflow, forum, open source
Theo dõi trend: Cloud MongoDB, serverless, microservices
Soft skills: teamwork, problem-solving, code readability
8. FAQ
MongoDB là gì?
Cơ sở dữ liệu NoSQL dạng document, lưu trữ JSON/BSON, linh hoạt, scale dễ dàng.
CRUD trong MongoDB gồm những gì?
Create, Read, Update, Delete - thao tác quản lý dữ liệu cơ bản.
Aggregation và Index trong MongoDB dùng để làm gì?
Aggregation xử lý, nhóm, tính toán dữ liệu. Index tăng tốc truy vấn, tối ưu performance.
Dev mới nên học MongoDB thế nào?
Cài đặt -> CRUD -> query -> aggregation -> index -> mini project -> GitHub portfolio -> freelance/remote.
9. Kết Luận
MongoDB là công cụ quan trọng cho mọi Dev: web, mobile, SaaS, realtime app, big data
Nắm CRUD, aggregation, index -> viết query chuẩn, tối ưu performance
Thực hành mini project, GitHub portfolio, deploy demo, freelance -> tăng kỹ năng và cơ hội việc làm
Thông điệp: MongoDB giúp Dev linh hoạt xử lý dữ liệu, scale hệ thống và phát triển sự nghiệp lâu dài trong thời đại AI




