This parser is generated using pegjs. It parses a string containing a MongoDB aggregation pipeline and returns an AST. The AST Types are defined in src/ast-types.ts. This package is compatible with CommonJS and ESM.
Allow Maggregor to parse request pipelines on MongoDB. Maggregor is a simple data-layer that speeds up MongoDB queries: https://github.com/maggregor/maggregor
npm install ts-mongo-aggregation-parser
yarn add ts-mongo-aggregation-parser
pnpm add ts-mongo-aggregation-parser
import { astTypes, parse } from 'ts-mongo-aggregation-parser'
const pipeline = `[
{
$group: {
_id: '$name',
count: {
$sum: 1,
},
},
},
]`
const ast = parse(pipeline)
The Visitor AST provides a good way to traverse and manipulate our custom Abstract Syntax Trees (ASTs). To use the AST visitor, simply extend the BaseASTVisitor
class and override the methods for the specific AST node types you're interested in. Below is a brief example:
import {
ASTAggregationExpression,
ASTField,
ASTProperty,
ASTStageGroup,
ASTStageList,
ASTVisitor,
BaseASTVisitor,
} from './ts-mongo-aggregation-parser'
class CustomASTVisitor extends BaseASTVisitor {
visitStageGroup(stageGroup: ASTStageGroup): void {
console.log(`StageGroup ID: ${stageGroup.id.name}`)
}
visitAggregationExpression(agg: ASTAggregationExpression): void {
console.log(`Aggregation Operator: ${agg.operator}`)
}
}
// Assuming you have created an AST
const ast = new ASTStageList() /* your stages here */
// Use your custom visitor
const visitor = new CustomASTVisitor()
ast.accept(visitor)
- $sum
- $avg
- $max
- $min
- $push
- $addToSet
- $first
- $last
- $addToSet
- $group
- $project
- $match
- $limit
- $skip
- $unwind
- $sort