Registry / database / mongodb-aggregation-dts

mongodb-aggregation-dts

JSON →
library0.1.2jsnpmunverified

Type definitions for MongoDB aggregation pipelines and operators not provided by the native MongoDB driver (0.1.2). Extends Collection#aggregate with full type info for pipeline stages and operators like $lookup, $group, $unwind, etc. Works with TypeScript 5.0+ and mongodb driver 5.0+. Ships bundled TS types. No diagnostics due to loose native types. Intended for editor intellisense only; will be archived when native support lands (track NODE-5863). Alternative to manual $project typing or generic casts.

npm install mongodb-aggregation-dts
INSTALL
IMPORT
SIG · MONGODB-AGGREGATIO
M
mongodb-aggregation-dts
databasejavascriptv0.1.2
harness data pending
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 }>
Debug
Known issues
gotchaTypeScript may show loose types if tsconfig doesn't include the package's types. The augmentation only works when tsconfig 'types' array includes 'mongodb-aggregation-dts' or a triple-slash reference is used.
fix
Ensure tsconfig.json includes 'mongodb-aggregation-dts' in the 'types' array, or add a reference directive in a .d.ts file.
affects: >=0.0.0
gotchaThe native MongoDB driver's aggregate method has a loose type (PipelineStage[]). This package overloads it, but diagnostics (e.g., catching invalid stages at compile time) may still not work perfectly because the overload uses union types that can be too permissive.
fix
Use explicit PipelineStage type annotations when needed for stricter checks: 'collection.aggregate<MyResult>(pipeline as PipelineStage[])'.
affects: >=0.0.0
breakingFuture native support (tracked in NODE-5863) may make this package obsolete. If you upgrade the mongodb driver beyond version 6, this package may conflict with native types.
fix
Monitor NODE-5863. When native types are released, remove this package and update tsconfig 'types' accordingly.
affects: >=0.1.2
deprecatedThe package's own npm install command in the README incorrectly says 'npm install mongodb-dts' instead of 'npm install mongodb-aggregation-dts'. This could cause confusion.
fix
Use the correct package name: 'npm install --save-dev mongodb-aggregation-dts'.
affects: >=0.0.0
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.
fix
Add 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.
fix
Use `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.
fix
Ensure 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.
fix
Remove direct import of .d.ts files. Use tsconfig 'types' array or a triple-slash reference instead.
Upgrade
Version history
0.1.2latest on npm
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
Agent activity
8 hits · last 30 days
node
8
Resources
mongodb-aggregation-dts — npm install mongodb-aggregation-dts · libregistry