Registry / database / mongodb-pipeline-factory

mongodb-pipeline-factory

JSON →
library0.23.2jsnpmunverified

A lightweight TypeScript-first library (v0.23.2, stable) for declaratively generating MongoDB aggregation pipelines. It provides composable helper functions like project, cond, and gt that reduce boilerplate and improve readability over raw pipeline objects. Compared to writing JSON manually, it offers validation, autocompletion, and integration with Node.js and Mongoose. Released under MIT, with 100% test coverage and weekly updates on npm.

npm install mongodb-pipeline-factory
INSTALL
IMPORT
SIG · MONGODB-PIPELINE-F
M
mongodb-pipeline-factory
databasejavascriptv0.23.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.

cond
import { cond } from 'mongodb-pipeline-factory'
const cond = require('mongodb-pipeline-factory').cond
ESM-only package; require() is not supported.
project
import { project } from 'mongodb-pipeline-factory'
import project from 'mongodb-pipeline-factory/project'
Named export; default export or subpath imports are not available.
gt
import { gt } from 'mongodb-pipeline-factory'
const { gt } = require('mongodb-pipeline-factory')
ESM-only; require() will throw. Types are included.
lt
import { lt } from 'mongodb-pipeline-factory'
Named export. Same import pattern as other operators.

Builds an aggregation pipeline using project, cond, and gt to compute discounts based on quantity, then calculates total price.

import { project, cond, gt, sum } from 'mongodb-pipeline-factory'; const pipeline = project( ['item', 1], ['discount', cond( gt('$qty', 250), 30, 20 )], ['total', { $multiply: ['$price', { $subtract: [1, { $divide: ['$discount', 100] }] }] }] ); import { MongoClient } from 'mongodb'; const client = new MongoClient(process.env.MONGODB_URI ?? 'mongodb://localhost:27017'); await client.connect(); const db = client.db('test'); const coll = db.collection('orders'); const result = await coll.aggregate(pipeline).toArray(); console.log(result); await client.close();
Debug
Known issues
deprecatedDeprecated function 'if' will be removed in v1.0. Use 'cond' instead.
fix
Replace `if(condition, trueVal, falseVal)` with `cond(condition, trueVal, falseVal)`.
affects: >=0.18.0 <1.0.0
breakingIn v0.20.0, the 'match' function was renamed to 'filter' to avoid conflicts with the MongoDB $match stage.
fix
Replace `match(...)` with `filter(...)`.
affects: >=0.20.0
gotchaFunctions like 'project' return plain JavaScript objects, not special pipeline objects. They can be spread into larger pipelines but cannot be passed to require() or treated as constructors.
fix
Use spread operator: `const pipeline = [...project(...), ...group(...)]`
affects: >=0.0.0
gotchaOperators such as 'gt' and 'lt' accept field path as string with '$' prefix; omitting the '$' will treat it as a literal string, not a field reference.
fix
Use `gt('$fieldName', value)` not `gt('fieldName', value)`
affects: >=0.0.0
breakingIn v0.19.0, the 'addFields' function was removed in favor of 'add' which matches MongoDB's $addFields (stage name unchanged, but function name changed).
fix
Replace `addFields(...)` with `add(...)`.
affects: >=0.19.0
Errors
Common errors & fixes
TypeError: _mongodb_pipeline_factory.cond is not a function
Using CommonJS require() instead of ESM import.
fix
Change to: `import { cond } from 'mongodb-pipeline-factory'`
SyntaxError: The requested module 'mongodb-pipeline-factory' does not provide an export named 'if'
The deprecated `if` export was removed; use `cond`.
fix
Replace `if` with `cond` in imports and usage.
MongoServerError: unknown operator: $cond (... pipeline object not wrapped correctly)
Using functions like `cond` inside template literals or concatenation, resulting in malformed BSON.
fix
Ensure the result of factory functions is used directly, not converted to string or JSON manually.
Upgrade
Version history
0.23.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Resources
mongodb-pipeline-factory — npm install mongodb-pipeline-factory · libregistry