Mongoose là ODM (Object Document Mapper) cho MongoDB — thêm schema, validation và middleware lên trên MongoDB vốn không ràng buộc cấu trúc.
Vài khái niệm cốt lõi:
- Middleware (hooks): chạy quanh thao tác — pre/post save, pre/post find... Dùng cho audit log, hash password.
- Virtuals: thuộc tính tính sẵn không lưu trong DB — vd fullName ghép từ firstName + lastName. Bật ra JSON bằng toJSON({ virtuals: true }).
- populate(): thay reference ObjectId bằng document thật — populate('author') lấy nguyên User. Cẩn thận N+1; kèm select để giới hạn field.
- lean(): trả về object JS thuần thay vì document Mongoose — nhanh hơn 2-3x; dùng khi chỉ cần đọc.
Lưu ý: Mongoose buffer các lệnh trước khi kết nối xong — phải xử lý lỗi kết nối cho đàng hoàng, kẻo lệnh treo lặng lẽ.
Mongoose is an ODM (Object Document Mapper) for MongoDB — it adds schema, validation, and middleware on top of MongoDB, which is otherwise structure-free.
A few core concepts:
- Middleware (hooks): run around operations — pre/post save, pre/post find... Used for audit logging, password hashing.
- Virtuals: computed properties not stored in the DB — e.g. fullName from firstName + lastName. Expose them in JSON with toJSON({ virtuals: true }).
- populate(): swaps an ObjectId reference for the real document — populate('author') fetches the whole User. Watch for N+1; add select to limit fields.
- lean(): returns plain JS objects instead of Mongoose documents — 2-3x faster; use for read-only queries.
Pitfall: Mongoose buffers commands before the connection is ready — handle connection errors properly, or commands hang silently.