A powerful TypeScript package that provides a debugging wrapper for Mongoose aggregation operations. Execute aggregation pipelines stage by stage and see the results after each stage for easier debugging, optimization, and understanding of your MongoDB aggregation queries.
Created by: Vikas Verma
MongoDB aggregation pipelines can be complex and hard to debug. This package helps you:
- β Debug Complex Pipelines: See exactly what each stage produces
- β Optimize Performance: Identify slow stages with execution timing
- β Validate Logic: Ensure each stage works as expected before moving to the next
- β Learn Aggregation: Understand how data flows through your pipeline
- β TypeScript Support: Full type safety and IntelliSense
- β Zero Configuration: Works with your existing Mongoose models
npm install mongoose-aggregation-wrapper
- Node.js >= 14
- Mongoose >= 7.0 (Compatible with Mongoose v7, v8, and future versions)
- TypeScript (if using TypeScript)
// 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)
}
Option | Type | Default | Description |
---|---|---|---|
debug |
boolean |
true |
When true , executes pipeline stage by stage. When false , executes full pipeline at once |
logResults |
boolean |
true |
When true , shows sample documents after each stage |
allowDiskUse |
boolean |
false |
MongoDB option for handling large datasets that exceed memory limits |
import Wrapper from 'mongoose-aggregation-wrapper';
class ProductService {
async getProductsWithDetails(ProductModel) {
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: Unwind category
{ $unwind: { path: '$category', preserveNullAndEmptyArrays: true } },
// Stage 6: Lookup reviews
{
$lookup: {
from: 'reviews',
localField: '_id',
foreignField: 'productId',
as: 'reviews'
}
},
// Stage 7: Calculate average rating
{
$addFields: {
averageRating: { $avg: '$reviews.rating' },
reviewCount: { $size: '$reviews' }
}
},
// Stage 8: Project final fields
{
$project: {
name: 1,
price: 1,
category: '$category.name',
averageRating: 1,
reviewCount: 1,
imageUrl: 1
}
}
];
// Debug mode - see results after each stage
return await Wrapper(ProductModel, pipeline, {
debug: true,
logResults: true,
allowDiskUse: true
});
}
}
async function getUserAnalytics(UserModel) {
const pipeline = [
// Stage 1: Match users from last 30 days
{
$match: {
createdAt: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
}
},
// Stage 2: Group by registration date
{
$group: {
_id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } },
userCount: { $sum: 1 },
users: { $push: "$$ROOT" }
}
},
// Stage 3: Sort by date
{ $sort: { _id: 1 } },
// Stage 4: Add cumulative count
{
$group: {
_id: null,
dailyStats: { $push: "$$ROOT" },
totalUsers: { $sum: "$userCount" }
}
}
];
return await Wrapper(UserModel, pipeline, {
debug: true,
logResults: false // Don't log user details for privacy
});
}
// For production - execute full pipeline without debug info
async function getProductionData(Model) {
const pipeline = [
{ $match: { status: 'active' } },
{ $sort: { createdAt: -1 } },
{ $limit: 100 }
];
return await Wrapper(Model, pipeline, {
debug: false, // No stage-by-stage execution
allowDiskUse: true
});
}
When debug: true
, you'll see detailed output like this:
π Starting Aggregation Pipeline Debug Mode
π Total stages: 4
==================================================
π Stage 1/4:
Stage content: { "$match": { "status": "active", "deleted": false } }
β±οΈ Execution time: 23ms
π Results count: 1,247
π Sample result (first document):
{
"_id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "Premium Laptop",
"status": "active",
"deleted": false,
"price": 1299.99
}
... and 1,246 more documents
----------------------------------------
π Stage 2/4:
Stage content: { "$sort": { "popularity": -1, "createdAt": -1 } }
β±οΈ Execution time: 15ms
π Results count: 1,247
π Sample result (first document):
{
"_id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "Most Popular Laptop",
"popularity": 98.5,
"createdAt": "2025-08-15T10:30:00.000Z"
}
... and 1,246 more documents
----------------------------------------
π Stage 3/4:
Stage content: { "$limit": 20 }
β±οΈ Execution time: 2ms
π Results count: 20
π Sample result (first document):
{
"_id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "Most Popular Laptop",
"popularity": 98.5
}
... and 19 more documents
----------------------------------------
π Stage 4/4:
Stage content: { "$lookup": { "from": "categories", ... } }
β±οΈ Execution time: 45ms
π Results count: 20
π Sample result (first document):
{
"_id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "Most Popular Laptop",
"category": [
{
"_id": "64f8a1b2c3d4e5f6a7b8c9d1",
"name": "Electronics"
}
]
}
... and 19 more documents
----------------------------------------
β
Pipeline execution completed successfully!
π― Final result count: 20
import express from 'express';
import Wrapper from 'mongoose-aggregation-wrapper';
import { ProductModel } from './models';
const router = express.Router();
router.get('/products', async (req, res) => {
try {
const { page = 1, limit = 10, category } = req.query;
const skip = (page - 1) * limit;
const pipeline = [
...(category ? [{ $match: { categoryId: category } }] : []),
{ $match: { deleted: false } },
{ $sort: { createdAt: -1 } },
{ $skip: skip },
{ $limit: parseInt(limit) },
{
$lookup: {
from: 'categories',
localField: 'categoryId',
foreignField: '_id',
as: 'category'
}
}
];
const products = await Wrapper(ProductModel, pipeline, {
debug: process.env.NODE_ENV === 'development',
logResults: false
});
res.json({ products, page, limit });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import Wrapper from 'mongoose-aggregation-wrapper';
@Injectable()
export class ProductService {
constructor(
@InjectModel('Product') private productModel: Model<any>
) {}
async findProductsWithAnalytics() {
const pipeline = [
{ $match: { status: 'active' } },
{
$lookup: {
from: 'orders',
localField: '_id',
foreignField: 'productId',
as: 'orders'
}
},
{
$addFields: {
totalSales: { $sum: '$orders.quantity' },
revenue: { $sum: '$orders.total' }
}
}
];
return await Wrapper(this.productModel, pipeline);
}
}
// β Don't do this - may cause memory issues
const hugePipeline = [
{ $match: {} }, // Matches millions of documents
// ... more stages
];
// β
Do this instead
const optimizedPipeline = [
{ $match: { createdAt: { $gte: recentDate } } }, // Filter first
{ $limit: 1000 }, // Limit early
// ... other stages
];
await Wrapper(Model, optimizedPipeline, { allowDiskUse: true });
// β Don't log sensitive user data
await Wrapper(UserModel, pipeline, {
debug: true,
logResults: true // This might log passwords, emails, etc.
});
// β
Disable result logging for sensitive data
await Wrapper(UserModel, pipeline, {
debug: true,
logResults: false // Still see stage info, but no sample data
});
// β
Use environment-based configuration
await Wrapper(Model, pipeline, {
debug: process.env.NODE_ENV === 'development',
logResults: process.env.NODE_ENV === 'development',
allowDiskUse: true
});
# Clone the repository
git clone https://github.com/vikasdev8/mongoose-aggregation-wrapper.git
cd mongoose-aggregation-wrapper
# Install dependencies
npm install
# Install dev dependencies for local testing
npm install --save-dev @types/node mongoose
npm run build # Build TypeScript to dist/
npm run dev # Run example with ts-node
npm run lint # Lint code with ESLint
npm run test # Run tests (when implemented)
npm run clean # Clean dist/ directory
npm run clean
npm run build
npm publish --access public
This package is written in TypeScript and provides full type definitions:
import Wrapper, { WrapperOptions } from 'mongoose-aggregation-wrapper';
// Full type safety
const options: WrapperOptions = {
debug: true,
logResults: false,
allowDiskUse: true
};
// Generic type support
interface User {
_id: string;
name: string;
email: string;
}
const users: User[] = await Wrapper<User>(UserModel, pipeline, options);
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature
- Commit your changes:
git commit -m 'Add some amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Vikas Verma
- GitHub: @vikasdev8
- npm: mongoose-aggregation-wrapper
If you have any questions or need help, please:
- Check the examples above
- Open an issue on GitHub
- Read the MongoDB Aggregation Documentation
If this package helped you debug your aggregation pipelines, please give it a β on GitHub!
If this package saved you time and helped you debug complex aggregation pipelines, consider supporting the project:
Other ways to support:
- β Star the repository on GitHub
- π Report bugs and suggest features
- π’ Share with your developer friends
- π» Contribute code improvements
- π Write about it in your blog
Your support helps maintain and improve this project! π
Happy Debugging! π