Debug MongoDB aggregation pipelines stage by stage with TypeScript support
MongoDB aggregation pipelines can be complex and hard to debug. This package helps you understand exactly what happens at each stage of your pipeline.
See exactly what each stage produces with detailed execution timing and sample results.
Identify slow stages and bottlenecks in your aggregation pipelines.
Ensure each stage works as expected before moving to the next.
Understand how data flows through your pipeline step by step.
Full type safety and IntelliSense for better development experience.
Works with your existing Mongoose models without any setup.
Prerequisites: Node.js ≥ 14, Mongoose ≥ 7.0
// ES6/TypeScript import Wrapper from 'mongoose-aggregation-wrapper'; // CommonJS const Wrapper = require('mongoose-aggregation-wrapper').default; // Use with your existing Mongoose model const results = await Wrapper(YourModel, pipeline);
import Wrapper from 'mongoose-aggregation-wrapper'; async function getUsersWithPosts(UserModel) { const pipeline = [ { $match: { active: true } }, { $sort: { createdAt: -1 } }, { $limit: 10 }, { $lookup: { from: 'posts', localField: '_id', foreignField: 'userId', as: 'posts' } } ]; // This will execute each stage and show debug info const results = await Wrapper(UserModel, pipeline); return results; }
Wrapper<T = any>( model: mongoose.Model, // Your Mongoose model pipeline: PipelineStage[], // Array of aggregation stages options?: WrapperOptions // Optional configuration ): Promise<T[]>
interface WrapperOptions { allowDiskUse?: boolean; // MongoDB allowDiskUse option (default: false) debug?: boolean; // Enable step-by-step execution (default: true) logResults?: boolean; // Log sample results after each stage (default: true) }
Default: true
When true, executes pipeline stage by stage. When false, executes full pipeline at once.
Default: true
When true, shows sample documents after each stage execution.
Default: false
MongoDB option for handling large datasets that exceed memory limits.
const pipeline = [ // Stage 1: Filter active products { $match: { status: 'active', deleted: false } }, // Stage 2: Sort by popularity { $sort: { popularity: -1, createdAt: -1 } }, // Stage 3: Pagination { $skip: 0 }, { $limit: 20 }, // Stage 4: Lookup category details { $lookup: { from: 'categories', localField: 'categoryId', foreignField: '_id', as: 'category' } }, // Stage 5: Calculate average rating { $addFields: { averageRating: { $avg: '$reviews.rating' }, reviewCount: { $size: '$reviews' } } } ]; const products = await Wrapper(ProductModel, pipeline, { debug: true, logResults: true, allowDiskUse: true });
// For production - execute full pipeline without debug info const results = await Wrapper(Model, pipeline, { debug: process.env.NODE_ENV === 'development', logResults: process.env.NODE_ENV === 'development', allowDiskUse: true });
If this package helped you debug your aggregation pipelines and saved you time, consider buying me a coffee! Your support helps me maintain and improve this project.