MongoDB PipelineJS
Aggregation Syntax for Javascript; Use Javascript syntax—instead of JSON—to
compose MongoDB aggregations.
Browse Reference Documentation »
Installation
Add mongodb-pipelinejs
to your MongoDB project:
With Yarn: yarn add mongodb-pipelinejs
With NPM: npm install mongodb-pipelinejs
Usage
Example needs refinement...
const $ = require('mongodb-pipelinejs');
mongoDB.collection('transactions').aggregate([
$.match({
userId: MY_USER_ID,
amount: $.gte(100),
type: $.in(['sale', 'transfer']),
status: $.neq('new'),
}),
$.redact($.switch('$$PRUNE')
.case($.eq('$type', 'sale')),
}),
$.addFields({
payments: $.filter('$payments', 'payment', $.in('$$payment.status', ['complete', 'approved'])),
}),
$.unwind('$payments'),
$.group({
_id: '$transactionId',
payments: $.push('$payments.paymentId'),
amountDue: $.last('$amount'),
amountPaid: $.sum('$payments.amount'),
})
$.unwind('$payments', true)
]).toArray();