Загрузка...

Mongoose Aggregation Middleware | Automatically Filter Deleted Documents in Express.js | Part #129

Aggregation middleware allows you to run custom logic before (pre) or after (post) an aggregation pipeline executes. In this tutorial, we solve a common real-world problem: ensuring that deleted documents (soft deletes) are automatically excluded from all aggregation results without having to manually add a $match stage to every single controller.

🚀 Topics Covered
✔️ Introduction to the aggregate middleware type.
✔️ Accessing the aggregation object using the this keyword.
✔️ Using this.pipeline() to inspect the current stages of the aggregation.
✔️ Implementing a global filter for "soft-deleted" documents.
✔️ Using the JavaScript unshift() method to inject stages at the start of a pipeline.
✔️ Difference between pre('aggregate') and post('aggregate').
✔️ Brief overview of Model Middleware for bulk operations.

💡 Main Point to Remember
Inside a pre-aggregation middleware, the this keyword refers to the Aggregate object. By using this.pipeline().unshift(), you can dynamically modify the aggregation array for every query in your application, ensuring consistent data rules (like "don't show deleted items") across all your endpoints.

⏳ Timeline
0:01 – Introduction to Aggregation Middleware.
0:49 – Demonstrating the problem: Aggregation includes deleted documents.
2:06 – Defining the pre('aggregate') middleware in the Hotel Schema.
3:38 – Inspecting the pipeline property in the terminal.
4:50 – Problem: Manually adding $match to every controller is inefficient.
6:34 – Solution: Dynamically injecting a $match stage using unshift().
7:40 – Verifying the results in Postman (count drops from 38 to 33).
8:30 – Confirming the fix across multiple endpoints (featured hotels, hotels by type).
9:44 – Exploring post('aggregate') middleware and accessing the result.
12:12 – Summary of Mongoose middleware types (Document, Query, Aggregation, and Model).

📖 Video Summary
The lecture focuses on the power of centralized logic. We start by noticing that our "Get Hotels by City" aggregation is counting hotels that were supposed to be hidden (soft-deleted).

Instead of updating every single route handler, we use Mongoose's pre('aggregate') hook. By calling this.pipeline().unshift({ $match: { isDeleted: false } }), we effectively tell Mongoose: "Before you do any grouping or sorting, always filter out deleted records first." The lesson also briefly touches on post middleware, which is useful if you need to log or modify the final calculated data after the database has finished its work.

🏷️ Video Related Tags
#NodeJS #Mongoose #ExpressJS #AggregationPipeline #Middleware #BackendDevelopment #JavaScript #CleanCode #DatabaseDesign #SoftDelete #ProcademyClasses #WebDevelopment

Видео Mongoose Aggregation Middleware | Automatically Filter Deleted Documents in Express.js | Part #129 канала procademy
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять