Nâng CaoDatabase iconDatabase

MongoDB aggregation pipeline là gì?

Aggregation pipeline là series of stages xử lý documents tuần tự, output của stage này là input của stage tiếp theo.

  • Stages phổ biến: $match (filter — đặt đầu pipeline để tận dụng index), $group (group và aggregate với $sum, $avg, $push), $project (reshape, add computed fields), $sort/$limit/$skip, $lookup (LEFT JOIN với collection khác).
  • Pipeline optimization: đặt $match và $project sớm nhất có thể để giảm số documents xử lý ở stages sau; $match sau $unwind là anti-pattern. $facet: chạy nhiều pipeline stages song song trên cùng input, trả về multiple result sets trong một query — phù hợp faceted search (count by category + count by price range simultaneously). $graphLookup: recursive lookup cho graph/tree data, ví dụ organizational hierarchy hay category tree với maxDepth control.
  • Atlas Aggregation Builder: GUI tool trong MongoDB Atlas cho phép build pipeline visually và export code.
  • Explain plan: db.collection.aggregate([...]).explain('executionStats') để kiểm tra query plan và indexes used.

Xem toàn bộ Database cùng filter theo level & chủ đề con.

Mở danh sách Database