How loading operators and Context works
mainBy default, importing from mingo loads all operators, which can prevent tree-shaking in bundlers. To optimize your bundle size, you can import from base modules (e.g., mingo/core, mingo/aggregator) and manually register only the operators you need using a Context object.
Operators loaded into a Context are immutable. Registering an existing operator name is a no-op and does not throw an error.
import { Context } from "mingo/core";
import { Aggregator } from "mingo/aggregator";
import { $match, $count } from "mingo/operators/pipeline";
import { $gt } from "mingo/operators/expression/comparison";
// creates a context with only operators needed for execution.
const context = Context.init({
pipeline: { $count, $match },
expression: { $gt }
});
const agg = new Aggregator(
[{ $match: { score: { $gt: 80 } } }, { $count: "passing_scores" }],
{ context } // pass context as part of options
);
const result = agg.run([
{ _id: 1, score: 10 },
{ _id: 2, score: 60 },
{ _id: 3, score: 100 }
]);