Registry /
database / mongodb-aggregation-dts
Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Collection
✓ import { Collection } from 'mongodb'
✗ import { Collection } from 'mongodb-aggregation-dts'
This package augments the native driver's types, does not re-export them. Import Collection from 'mongodb' as usual.
AggregationPipelineStage
✓ import type { AggregationPipelineStage } from 'mongodb-aggregation-dts'
✗ import { AggregationPipelineStage } from 'mongodb-aggregation-dts'
Exported as a type (not a value). Use `import type` to avoid runtime errors. Available for explicit stage type annotations.
PipelineStage
✓ /// <reference types="mongodb-aggregation-dts" />
import { PipelineStage } from 'mongodb'
✗ import { PipelineStage } from 'mongodb-aggregation-dts'
Declared in the 'mongodb' module via augmentation. The triple-slash reference or adding the package to tsconfig's 'types' array is required to load the augmentation.
Shows installation, tsconfig setup, and typed aggregate pipeline with $match, $group, $sort.
// 1. Install: npm install --save-dev mongodb-aggregation-dts
// 2. Add to tsconfig.json:
{
"compilerOptions": {
"types": ["mongodb", "mongodb-aggregation-dts"]
}
}
// 3. Use in code:
import { MongoClient } from 'mongodb';
const client = new MongoClient(process.env.MONGO_URI ?? 'mongodb://localhost:27017');
const db = client.db('test');
const collection = db.collection<{ name: string; age: number }>('users');
// Now aggregate() has full type inference for stages:
const pipeline = [
{ $match: { age: { $gte: 18 } } },
{ $group: { _id: '$name', total: { $sum: 1 } } },
{ $sort: { total: -1 } }
];
const results = await collection.aggregate(pipeline).toArray();
// results is typed as Array<{ _id: string; total: number }>
Errors
Common errors & fixes
Type 'PipelineStage[]' is not assignable to parameter of type 'Document[]'.
The native driver expects Document[] for the aggregate pipeline parameter, but PipelineStage[] from 'mongodb' is a union type that may not be directly compatible.
fixAdd a type assertion: `collection.aggregate(pipeline as Document[])` or use the overloaded signature provided by this package (ensure augmentation is loaded).
Cannot find name 'AggregationPipelineStage'.
The symbol AggregationPipelineStage is exported from 'mongodb-aggregation-dts' but is a type-only export. Using a regular import without 'type' keyword causes a runtime error or TS error depending on module setting.
fixUse `import type { AggregationPipelineStage } from 'mongodb-aggregation-dts'`. Overload signature is not compatible with implementation signature.
Conflicting overloads when both native and this package define aggregate overloads, typically due to version mismatch or type augmentation order.
fixEnsure mongodb driver is version 5.x and TypeScript is 5.0+. Remove any other augmentation packages that also overload Collection#aggregate.
An import path cannot end with a '.d.ts' extension. Consider importing 'mongodb-aggregation-dts/types' instead.
Trying to import from the package's types file directly (e.g., './node_modules/mongodb-aggregation-dts/types/index.d.ts') instead of relying on the package's main types entry.
fixRemove direct import of .d.ts files. Use tsconfig 'types' array or a triple-slash reference instead.
Audit
Dependencies
mongodbrequiredPeer dependency — overrides the aggregate method on Collection prototype
typescriptrequiredPeer dependency — requires TS 5.0+ for template literal types and mapped types used in deep stage inference