Mongo Pipeline Kit
A feature-rich MongoDB pipeline builder kit for creating, reusing, and managing aggregation pipelines with ease. Now with enhanced JSON support, pipeline utilities, and advanced analysis capabilities.
Features
- 🏗️ Object-oriented pipeline building
- 🔄 Reusable pipeline components
- 🎯 Type-safe pipeline construction
- 📦 Pipeline composition and chaining
- 🔍 Built-in pipeline validation
- 📝 Comprehensive pipeline documentation
- 🧩 Modular and extensible design
- 📄 NEW: JSON export/import functionality
- 🛠️ NEW: Advanced pipeline utilities and analysis
- 📊 NEW: Pipeline statistics and complexity estimation
- 🔄 NEW: Pipeline comparison and manipulation tools
Installation
npm install mongo-pipeline-kit
Quick Start
import { PipelineBuilder } from "mongo-pipeline-kit";
const builder = new PipelineBuilder();
const pipeline = builder
.match({ status: "active" })
.group({
_id: "$category",
total: { $sum: 1 },
items: { $push: "$$ROOT" },
})
.sort({ total: -1 })
.limit(10)
.build();
const results = await collection.aggregate(pipeline).toArray();
JSON Export/Import
Export Pipeline to JSON
import { PipelineBuilder } from "mongo-pipeline-kit";
const builder = new PipelineBuilder()
.match({ status: "active" })
.group({ _id: "$category", count: { $sum: 1 } })
.sort({ count: -1 })
.limit(10);
const jsonString = builder.toJSON();
console.log(jsonString);
const prettyJson = builder.toJSON(true);
console.log(prettyJson);
const exportData = builder.exportWithMetadata({
description: "Active users by category",
author: "John Doe",
version: "1.0.0",
});
Import Pipeline from JSON
import { PipelineUtils } from "mongo-pipeline-kit";
const jsonString = `[
{"$match": {"status": "active"}},
{"$group": {"_id": "$category", "count": {"$sum": 1}}},
{"$sort": {"count": -1}},
{"$limit": 10}
]`;
const pipeline = PipelineUtils.fromJSON(jsonString);
const builder = new PipelineBuilder();
pipeline.forEach((stage) => builder.addStage(stage));
Pipeline Utilities
Pipeline Analysis
import { PipelineUtils } from "mongo-pipeline-kit";
const pipeline = [
{ $match: { status: "active" } },
{ $group: { _id: "$category", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
{ $limit: 10 },
];
const stats = PipelineUtils.getStats(pipeline);
console.log(stats);
const description = PipelineUtils.describe(pipeline);
console.log(description);
Pipeline Manipulation
const clonedPipeline = PipelineUtils.clone(pipeline);
const matchStages = PipelineUtils.filterByStageType(pipeline, "$match");
const pipelineWithoutLimit = PipelineUtils.removeStageType(pipeline, "$limit");
const newPipeline = PipelineUtils.insertStage(pipeline, 1, {
$addFields: { processed: true },
});
const modifiedPipeline = PipelineUtils.replaceStage(pipeline, 0, {
$match: { status: "inactive" },
});
Pipeline Comparison
const pipeline1 = [
{ $match: { status: "active" } },
{ $group: { _id: "$category", count: { $sum: 1 } } },
];
const pipeline2 = [
{ $match: { status: "active" } },
{ $group: { _id: "$category", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
];
const comparison = PipelineUtils.compare(pipeline1, pipeline2);
console.log(comparison);
Advanced Usage
Creating Reusable Pipeline Components
import { PipelineBuilder, PipelineStage } from "mongo-pipeline-kit";
const activeUsersStage: PipelineStage = {
$match: {
status: "active",
lastLogin: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) },
},
};
const userStatsPipeline = builder
.addStage(activeUsersStage)
.group({
_id: "$role",
count: { $sum: 1 },
})
.build();
Pipeline Composition
import { PipelineBuilder, PipelineComposer } from "mongo-pipeline-kit";
const composer = new PipelineComposer();
const filteringPipeline = builder.match({ status: "active" }).build();
const aggregationPipeline = builder
.group({
_id: "$category",
total: { $sum: "$amount" },
})
.build();
const finalPipeline = composer
.compose(filteringPipeline, aggregationPipeline)
.build();
const exportData = composer.exportWithMetadata({
description: "Composed pipeline for user analytics",
tags: ["analytics", "users"],
});
API Documentation
PipelineBuilder
The main class for building MongoDB aggregation pipelines.
Core Methods
addFields(fields: object)
: Add a $addFields
stage
addStage(stage: PipelineStage)
: Add a custom pipeline stage
bucket(options: object)
: Add a $bucket
stage
bucketAuto(options: object)
: Add a $bucketAuto
stage
build()
: Build the final pipeline array
collStats(options: object)
: Add a $collStats
stage
count(fieldName: string)
: Add a $count
stage
facet(options: object)
: Add a $facet
stage
geoNear(options: object)
: Add a $geoNear
stage
graphLookup(options: object)
: Add a $graphLookup
stage
group(expression: object)
: Add a $group
stage
indexStats(options: object)
: Add a $indexStats
stage
limit(n: number)
: Add a $limit
stage
lookup(options: object)
: Add a $lookup
stage
match(condition: object)
: Add a $match
stage
merge(options: object)
: Add a $merge
stage
out(collection: string | object)
: Add an $out
stage
project(projection: object)
: Add a $project
stage
redact(expression: object)
: Add a $redact
stage
replaceRoot(newRoot: object)
: Add a $replaceRoot
stage
replaceWith(newRoot: object)
: Add a $replaceWith
stage
sample(options: { size: number })
: Add a $sample
stage
setWindowFields(options: object)
: Add a $setWindowFields
stage
skip(n: number)
: Add a $skip
stage
sort(sort: object)
: Add a $sort
stage
sortByCount(expression: any)
: Add a $sortByCount
stage
unionWith(options: string | object)
: Add a $unionWith
stage
unset(fields: string | string[])
: Add an $unset
stage
unwind(field: string | object)
: Add a $unwind
stage
New JSON Export Methods
toJSON(pretty?: boolean)
: Convert pipeline to JSON string
toObject()
: Get pipeline as plain object
exportWithMetadata(metadata)
: Export pipeline with additional metadata
toString()
: Get human-readable string representation
Pipeline Management Methods
clear()
: Clear all stages from the pipeline
getStageCount()
: Get the current number of stages
getStage(index: number)
: Get a specific stage
replaceStage(index: number, stage: PipelineStage)
: Replace a stage
removeStage(index: number)
: Remove a stage
PipelineComposer
Utility class for composing and reusing pipeline segments.
Methods
compose(...pipelines: Pipeline[])
: Compose multiple pipelines
extend(basePipeline: Pipeline, extension: Pipeline)
: Extend an existing pipeline
validate(pipeline: Pipeline)
: Validate a pipeline structure
toJSON(pretty?: boolean)
: Convert composed pipeline to JSON
toObject()
: Get composed pipeline as plain object
exportWithMetadata(metadata)
: Export with metadata
toString()
: Get human-readable representation
hasStage(operator: string)
: Check if pipeline contains specific stage type
getStagesByType(operator: string)
: Get all stages of a specific type
PipelineUtils
Static utility class for pipeline operations and analysis.
Methods
fromJSON(jsonString: string)
: Parse JSON string to pipeline
toJSON(pipeline: Pipeline, pretty?: boolean)
: Convert pipeline to JSON
validate(pipeline: Pipeline, options?)
: Validate pipeline structure
clone(pipeline: Pipeline)
: Deep copy pipeline
getStats(pipeline: Pipeline)
: Get pipeline statistics
describe(pipeline: Pipeline)
: Get human-readable description
compare(pipeline1: Pipeline, pipeline2: Pipeline)
: Compare two pipelines
filterByStageType(pipeline: Pipeline, operator: string)
: Filter stages by type
removeStageType(pipeline: Pipeline, operator: string)
: Remove stages by type
insertStage(pipeline: Pipeline, index: number, stage: PipelineStage)
: Insert stage
replaceStage(pipeline: Pipeline, index: number, stage: PipelineStage)
: Replace stage
Examples
For comprehensive examples of all features, see EXAMPLES.md.
🐛 Issues & Support
Getting Help
If you encounter any issues or need help with mongo-pipeline-kit:
Issue Guidelines
- Use Templates: We provide issue templates for bugs, features, and questions
- Include Details: Provide environment info, code examples, and error messages
- Be Specific: Describe what you're trying to achieve and what went wrong
- Search First: Check existing issues to avoid duplicates
For detailed information about reporting issues, see ISSUES.md.
Contributing
We welcome contributions! Please see our Contributing Guide for details on how to get started.
Publishing
For information about publishing new versions, please refer to our Publishing Guide.
License
MIT