MongoDB Aggregate Pipeline Build

Build MongoDB aggregation pipelines with typed stages for filtering grouping and projection.

JavascriptData Fetching

Code

snippet
import{MongoClient}from'mongodb';export function aggregateBuilder(collection){const stages=[];const api={$match:(q)=>{stages.push({$match:q});return api;},$group:(g)=>{stages.push({$group:g});return api;},$sort:(s)=>{stages.push({$sort:s});return api;},$project:(p)=>{stages.push({$project:p});return api;},$limit:(n)=>{stages.push({$limit:n});return api;},$lookup:(l)=>{stages.push({$lookup:l});return api;},exec:async()=>collection.aggregate(stages).toArray()};return api;}export async function connect(url){const client=new MongoClient(url);await client.connect();return client.db();}