npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongodb-pipeline-builder

v4.0.1

Published

Pipeline constructor for the aggregate method of a mongoDB collection

Downloads

254

Readme

++

NPM version NPM npm

GitHub branch checks state CircleCI Sonar Quality Gate

Sonar Tests Sonar Coverage

GitHub top language Lines of Code Duplicated Lines (%)

GitHub code size in bytes GitHub commit activity GitHub last commit (branch)

Maintainability Rating Reliability Rating Security Rating


-> Technical documentation <-

mongodb-pipeline-builder

mongodb-pipeline-builder is a pipeline builder for the db.collection.aggregate, the db.aggregate and the mongoose Model.aggregate methods.

  • Simplify pipelines by making them more readable
  • Pipelines are easier to edit.
  • Pipeline stages appear in an array.
  • Sequential stages for documents

All stages except the Out, Merge, GeoNear, ChangeStream, ChangeStreamSplitLargeEvent and Paging stages can appear multiple times in a pipeline.


npm package

npm i -S mongodb-pipeline-builder


News and Breaking changes between v3 and v4

PipelineBuilder:

  • Renaming getPipeline() with build()
  • Added new stages: ChangeStream, ChangeStreamSplitLargeEvent, Densify, Documents, Fill, ListLocalSessions, ListSampledQueries, ListSearchIndexes, SearchMeta, SetWindowFields and ShardedDataDistribution.
  • Added the possibility to insert stages without validation with new Insert stage.
  • Checking for non-duplicable stages.

Helpers:

  • Replacing the Payload suffix with Helper suffix
  • Prefixed with the name of the pipeline stage where they should be used

Operators:

  • Prefixed with the $ symbol
  • Rename MapOperator to $Map

GetResult:

  • To be used if no Paging stage is set
  • Removing GetDocs method arguments
  • Added new GetElement method to the response object

GetPagingResult:

  • To be used exclusively with Paging stage.

Welcome generics! GetResult<Type> and GetPagingResult<Type> now offer the ability to type responses.


Usage:

Using require()

const PipelineBuilder = require("mongodb-pipeline-builder").PipelineBuilder;
const { LookupEqualityHelper, ProjectOnlyHelper, Field } = require('mongodb-pipeline-builder/helpers');
const { $LessThanEqual, $ArrayElementAt, $Equal, $Expression } = require('mongodb-pipeline-builder/operators');

Using import

import { PipelineBuilder } from 'mongodb-pipeline-builder';
import { LookupEqualityHelper, ProjectOnlyHelper, Field } from 'mongodb-pipeline-builder/helpers';
import { $LessThanEqual, $ArrayElementAt, $Equal, $Expression } from 'mongodb-pipeline-builder/operators';

Pagination example

const myNewPipeline = new PipelineBuilder( 'myPagination', { debug: true } )
    .Match( $Expression( $LessThanEqual( '$id', 20 ) ) )
    .Project( ProjectOnlyHelper( 'name', 'weight' ) )
    .Paging( 5, 3 ) // 5 per page, page 3
    .build();

is equivalent to

const myNewPipeline = [ {
    $facet: {
        docs: [
            { $match: { $expr: { $lte: ["$id", 20] } } },
            { $project: { _id: 0, name: 1, weight: 1 } },
            { $skip: 10 },
            { $limit: 5 }
        ],
        count: [
            { $match: { $expr: { $lte: ["$id", 20] } } },
            { $count: "totalElements" }
        ]
    }
} ];

No pagination example

const myNewPipeline = new PipelineBuilder( 'user-skills' )
    .Match( $Expression( $Equal( '$id', 123456 ) ) )
    .Lookup( LookupEqualityHelper( 'profiles', 'profile', 'id', 'profileId' ) )
    .Project( ProjectOnlyHelper( 'firstname', 'lastname', 'email' ) )
    .AddFields(
        Field( 'skills', $ArrayElementAt( '$profile.skills', 0 ) ),
        Field( 'availability', $ArrayElementAt( '$profile.availability', 0 ) )
    )
    .Unset( 'profile' )
    .build();

is equivalent to

const myNewPipeline = [
    { $match: { $expr: { $eq: ["$id", 123456] } } },
    { $lookup: { from: "profiles", as: "profile", localField: "id", foreignField: "profileId" } },
    { $project: { _id: 0, firstname: 1, lastname: 1, email: 1 } },
    { $addFields: {
        skills: { $arrayElemAt: ["$profile.skills", 0] },
        availability: { $arrayElemAt: ["$profile.availability", 0] }
    } },
    { $unset: "profile" }
];

GetResult method (No pagination)

GetResult<T>(): Promise<GetResultResponse<T>>

GetResult<T>() is an asynchronous method that provides a very easy way to use aggregation responses.

This method returns a GetResultResponse object that contains 3 methods:

  • GetDocs(): T[] to get all the documents that match the request.
  • GetElement(index: number | 'last'): T to get a particular document by its index.
  • GetCount(): number to get the total number of documents found.
const result = await GetResult<DocType>( target, pipeline ); 
result.GetDocs(); // () => DocType[]
result.GetElement(index | 'last'); // () => DocType | undefined
result.GetCount(); // () => number

$Or

GetResult<DocType>( target, pipeline ).then( result => {
    result.GetDocs(); // () => DocType[]
    result.GetElement(index | 'last'); // () => DocType | undefined
    result.GetCount(); // () => number
} );

GetElement(index: number | 'last') method possibilities:

  • A particular document can be retrieved by specifying its index.
  • To get the last document, simply provide the string 'last'.
  • If the specified index is greater than the index of the last document, GetElement() will return undefined.
// GetDocs() -> [document1, document2, document3, ..., document51]
result.GetElement(2); // will return document to index 2, document3
result.GetElement('last'); // will return the last document, document51
result.GetElement(99); // will return undefined

GetPagingResult method (Pagination)

GetPagingResult<T>(): Promise<GetPagingResultResponse<T>>

GetPagingResult<T>() is an asynchronous method that provides a very easy way to use aggregation responses when Paging stage is used.

This method returns a GetPagingResultResponse object that contains three methods:

  • GetDocs() to get the documents found.
  • GetCount() to get the total number of documents found.
  • GetTotalPageNumber() to get the total number of pages.
const result = await GetPagingResult<DocType>(target, pipeline);
result.GetDocs(); // () => DocType[]
result.GetCount(); // () => number
result.GetTotalPageNumber(); // () => number

$Or

GetPagingResult<DocType>(target, pipeline).then( result => {
    result.GetDocs(); // () => DocType[]
    result.GetCount(); // () => number
    result.GetTotalPageNumber(); // () => number
} );

=> Try the lib on NPM RunKit with the require method <=


// builder = new PipelineBuilder('example');

CUSTOM STAGES

Paging(elementsPerPage, page)

The Paging stage automatically adds 3 native stages used to paginate documents ($skip, $limit and $count). Page is optional and defaults to 1.

builder.Paging(5, 2).build();

// pipeline
[
  {
    $facet: {
      docs: [ { '$skip': 5 }, { '$limit': 5 } ],
      count: [ { '$count': 'totalElements' } ]
    }
  }
]

Insert(stage)

The Insert stage allows you to insert a stage without validation. Usefully when you need to insert a stage that is not yet implemented or when the value fails validation but for some reason you want to keep it.

builder.Insert({ '$myCustomStage': { myField: 'myValue' } }).build();

// pipeline
[ { $myCustomStage: { myField: 'myValue' } } ]

MONGODB STAGES

AddFields(...values)

Helper: Field(name, value)

builder.AddFields(
  Field('foo', 'value1'),
  Field('bar', 'value2'),
).build();

// pipeline
[ { $addFields: { foo: 'value1', bar: 'value2' } } ]

Bucket(value)

Helper: BucketHelper(groupBy, boundaries, optional)

builder.Bucket(BucketHelper('$age', [6, 13, 18])).build();

// pipeline
[ { $bucket: { groupBy: '$age', boundaries: [ 6, 13, 18 ] } } ]

BucketAuto(value)

Helper: BucketAutoHelper(groupBy, buckets, optional)

builder.BucketAuto(BucketAutoHelper('$age', 5)).build();

// pipeline
[ { $bucketAuto: { groupBy: '$age', buckets: 5 } } ]

ChangeStream(value)

Helper: ChangeStreamHelper(optional)

builder.ChangeStream(ChangeStreamHelper()).build();

// pipeline
[ { $changeStream: {} } ]

ChangeStreamSplitLargeEvent()

builder.ChangeStreamSplitLargeEvent().build();

// pipeline
[ { $changeStreamSplitLargeEvent: {} } ]

CollStats(value)

Helper: CollStatsHelper(optional)

builder.CollStats(CollStatsHelper()).build();

// pipeline
[ { $collStats: {} } ]

Count(value)

builder.Count('counter').build();

// pipeline
[ { $count: 'counter' } ]

CurrentOp(value)

Helper: CurrentOpHelper(optional)

builder.CurrentOp(CurrentOpHelper()).build();

// pipeline
[ { $currentOp: {} } ]

Densify(value)

Helper: DensifyHelper(field, range, optional)

builder.Densify(
    DensifyHelper(
      'altitude',
      { bounds: 'full', step: 200 },
      { partitionByFields: [ 'variety' ] }
    ),
).build();

// pipeline
[
  {
    $densify: {
      field: 'altitude',
      range: { bounds: 'full', step: 200 }
      partitionByFields: [ 'variety' ],
    }
  }
]

Documents(...values)

builder.Documents({ docId: 1 }, { docId: 2 }, { docId: 3 }).build();

// pipeline
[ { $documents: [ { docId: 1 }, { docId: 2 }, { docId: 3 } ] } ]

Facet(...values)

Helper: Field(name, pipeline)

builder.Facet(
  Field('pipeline1', [{ $match: { tag: 'first' }}]),
  Field('pipeline2', [{ $match: { tag: 'second' }}]),
  Field('pipeline3', [{ $match: { tag: 'third' }}]),
).build();

// pipeline
[
  {
    $facet: {
      pipeline1: [ { '$match': { tag: 'first' } } ],
      pipeline2: [ { '$match': { tag: 'second' } } ],
      pipeline3: [ { '$match': { tag: 'third' } } ]
    }
  }
]

Fill(value)

Helper: FillHelper(output, optional)

builder.Fill(
  FillHelper({
    bootsSold: { value: 0 },
    sandalsSold: { value: 0 },
    sneakersSold: { value: 0 },
  }),
).build();

// pipeline
[
  {
    $fill: {
      output: {
        bootsSold: { value: 0 },
        sandalsSold: { value: 0 },
        sneakersSold: { value: 0 }
      }
    }
  }
]

GeoNear(value)

Helper: GeoNearHelper(near, distanceField, optional)

builder.GeoNear(
  GeoNearHelper({ type: "Point", coordinates: [ -73.99279 , 40.719296 ] }, 'calculated')
).build();

// pipeline
[
  {
    $geoNear: {
      near: { type: 'Point', coordinates: [ -73.99279, 40.719296 ] },
      distanceField: 'calculated'
    }
  }
]

GraphLookup(value)

builder.GraphLookup({
  from: 'employees', startWith: '$reportsTo', connectFromField: 'reportsTo', connectToField: 'name', as: 'reportingHierarchy',
}).build();

// pipeline
[
  {
    $graphLookup: {
      from: 'employees',
      startWith: '$reportsTo',
      connectFromField: 'reportsTo',
      connectToField: 'name',
      as: 'reportingHierarchy'
    }
  }
]

Group(value)

builder.Group({ _id: null, count: { $count: { } } }).build();

// pipeline
[
  { $group: { _id: null, count: { '$count': {} } } }
]

IndexStats()

builder.IndexStats().build();

// pipeline
[ { $indexStats: {} } ]

Limit(value)

builder.Limit(10).build();

// pipeline
[ { $limit: 10 } ]

ListLocalSessions(value)

builder.ListLocalSessions({ allUsers: true }).build();

// pipeline
[ { $listLocalSessions: { allUsers: true } } ]

ListSampledQueries(value)

builder.ListSampledQueries({ namespace: "social.post" }).build();

// pipeline
[ { $listSampledQueries: { namespace: 'social.post' } } ]

ListSearchIndexes(value)

builder.ListSearchIndexes({ name: 'searchIndex01' }).build();

// pipeline
[ { $listSearchIndexes: { name: 'searchIndex01' } } ]

ListSessions(value)

builder.ListSessions({ allUsers: true }).build();

// pipeline
[ { $listSessions: { allUsers: true } } ]

Lookup(value)

Helper: LookupConditionHelper(from, as, optional)

builder.Lookup(LookupConditionHelper('users', 'users')).build();

// pipeline
[ { $lookup: { from: 'users', as: 'users' } } ]

Helper: LookupEqualityHelper(from, as, localField, foreignField)

builder.Lookup(
  LookupEqualityHelper('users', 'users', 'userId', 'id')
).build();

// pipeline
[
  {
    $lookup: {
      from: 'users',
      localField: 'userId',
      foreignField: 'id',
      as: 'users'
    }
  }
]

Match(value)

Helper: Field(name, value)

builder.Match(Field('age', 18)).build();

// pipeline
[ { $match: { age: 18 } } ]

Operator: $Expression

builder.Match($Expression($GreaterThanEqual('$age', 18))).build();

// pipeline
[ { $match: { '$expr': { '$gte': [ '$age', 18 ] } } } ]

Merge(value)

Helper: MergeHelper(into, optional)

builder.Merge(MergeHelper('newCollection')).build();

// pipeline
[ { $merge: { into: 'newCollection' } } ]

Out(value)

Helper: OutHelper(collection, optional)

builder.Out(OutHelper('users')).build();

// pipeline
[ { $out: 'users' } ]

PlanCacheStats()

builder.PlanCacheStats().build();

// pipeline
[ { $planCacheStats: {} } ]

Project(...values)

Helper: ProjectHelper(field, value)

builder.Project(
  ProjectHelper('age', '$user.age'),
  ProjectHelper(
    'nickname',
    {
      $cond: {
        if: { $eq: [ '', '$user.nickname' ] },
        then: '$$REMOVE',
        else: '$user.nickname',
      },
    },
  ),
).build();

// pipeline
[
  {
    $project: {
      age: '$user.age',
      nickname: {
        $cond: {
          if: { $eq: [ '', '$user.nickname' ] },
          then: '$$REMOVE',
          else: '$user.nickname'
        }
      }
    }
  }
]

Helper: ProjectIgnoreHelper(...fields)

builder.Project(ProjectIgnoreHelper('password', 'refreshToken')).build();

// pipeline
[ { $project: { password: 0, refreshToken: 0 } } ]

Helper: ProjectOnlyHelper(...fields)

builder.Project(ProjectOnlyHelper('password', 'refreshToken')).build();

// pipeline
[ { $project: { _id: 0, password: 1, refreshToken: 1 } } ]

Redact(value)

builder.Redact(
  $Condition(
    $GreaterThan($Size($SetIntersection('$tags', ['STLW', 'G'])), 0),
    '$$DESCEND',
    '$$PRUNE'
  )
).build();

// pipeline
[
  {
    $redact: {
      '$cond': [
        { '$gt': [ { '$size': { '$setIntersection': [ '$tags', [ 'STLW', 'G' ] ] } }, 0 ] },
        '$$DESCEND',
        '$$PRUNE'
      ]
    }
  }
]

ReplaceRoot(value)

builder.ReplaceRoot({
  newRoot: { full_name: { $concat : [ "$first_name", " ", "$last_name" ] } }
}).build();

// pipeline
[
  {
    $replaceRoot: {
      newRoot: {
        full_name: { '$concat': [ '$first_name', ' ', '$last_name' ] }
      }
    }
  }
]

ReplaceWith(value)

builder.ReplaceWith('$name').build();

// pipeline
[ { $replaceWith: '$name' } ]

Sample(value)

Helper: SampleHelper(size)

builder.Sample(SampleHelper(6)).build();

// pipeline
[ { $sample: { size: 6 } } ]

Search(value)

Helper: SearchHelper(operator | collector, optional)

builder.Search(
  SearchHelper({
    near: { path: 'released', origin: date, pivot: 7776000000 },
  }),
).build();

// pipeline
[
  {
    $search: {
      near: { path: 'released', origin: date, pivot: 7776000000 },
    },
  }
]

SearchMeta(value)

Helper: SearchMetaHelper(collector, optional)

builder.SearchMeta(
  SearchMetaHelper({
    facet: {
      operator: {
        near: { path: 'released', origin: date, pivot: 7776000000 },
      },
      facets: {
        test: { type: 'number', path: 'released', boundaries: [0, 100] },
      },
    },
  })
).build();

// pipeline
[
  {
    $searchMeta: {
      facet: {
        operator: {
          near: { path: 'released', origin: date, pivot: 7776000000 },
        },
        facets: {
          test: { type: 'number', path: 'released', boundaries: [0, 100] },
        },
      },
    }
  }
]

Set(...values)

Helper: Field(name, value)

builder.Set(Field('first', true), Field('second', 2)).build();

// pipeline
[ { $set: { first: true, second: 2 } } ]

SetWindowFields(value)

builder.SetWindowFields({
  partitionBy: "$state",
  sortBy: { orderDate: 1 },
  output: {
    cumulativeQuantityForState: {
      $sum: "$quantity",
      window: { documents: [ "unbounded", "current" ] }
    }
  }
}).build();

// pipeline
[
  {
    $setWindowFields: {
      partitionBy: '$state',
      sortBy: { orderDate: 1 },
      output: {
        cumulativeQuantityForState: {
          '$sum': '$quantity',
          window: { documents: [ 'unbounded', 'current' ] }
        }
      }
    }
  }
]

ShardedDataDistribution(value)

builder.ShardedDataDistribution({}).build();

// pipeline
[ { $shardedDataDistribution: {} } ]

Skip(value)

builder.Skip(100).build();

// pipeline
[ { $skip: 100 } ]

Sort(...values)

Helper: Field(name, value)

builder.Sort(
  Field('first', -1),
  Field('second', 1),
  Field('third', { $meta: "textScore" }),
).build();

// pipeline
[
  {
    $sort: { first: -1, second: 1, third: { '$meta': 'textScore' } }
  }
]

SortByCount(value)

builder.SortByCount('$employee').build();
// pipeline

[ { $sortByCount: '$employee' } ]

UnionWith(value)

Helper: UnionWithHelper(collection, pipeline)

builder.UnionWith(
  UnionWithHelper('cars'),
).build();

// pipeline
[
  {
    $unionWith: { coll: 'cars' }
  }
]
builder.UnionWith(
  UnionWithHelper(
    undefined,
    [{ $document: [{ ref: 1 }, { ref: 2 }, { ref: 3 }],  }]),
).build();

// pipeline
[
  {
    $unionWith: { pipeline: [ { '$document': [ { ref: 1 }, { ref: 2 }, { ref: 3 } ] } ] }
  }
]
builder.UnionWith(
  UnionWithHelper('cars', [{ $match: { color: 'red' } }]),
).build();

// pipeline
[
  {
    $unionWith: { coll: 'cars', pipeline: [ { '$match': { color: 'red' } } ] }
  }
]

Unset(...values)

builder.Unset('users', 'roles').build();

// pipeline
[ { $unset: [ 'users', 'roles' ] } ]

Unwind(value)

builder.Unwind({ path: '$sizes', preserveNullAndEmptyArrays: true }).build();

// pipeline
[ { $unwind: { path: '$sizes', preserveNullAndEmptyArrays: true } } ]

MONGODB OPERATORS

$Absolute

$Absolute(-5)

// operator
{ $abs: -5 }

$Accumulator

$Accumulator(
  () => ({ count: 0, sum: 0 }),
  (state: { count: number; sum: number; }, numCopies: number) => ({
    count: state.count + 1,
    sum: state.sum + numCopies,
  }),
  ['$copies'],
  (state1: { count: number; sum: number; }, state2: { count: number; sum: number; }) => ({
    count: state1.count + state2.count,
    sum: state1.sum + state2.sum,
  }),
  { finalize: (state: { sum: number; count: number; }) => (state.sum / state.count) },
)

// operator
{
  '$accumulator': {
    init: [ () => ({ count: 0, sum: 0 }) ],
    accumulate: [
      (state: { count: number; sum: number; }, numCopies: number) => ({
        count: state.count + 1,
        sum: state.sum + numCopies,
      })
    ],
    accumulateArgs: [ '$copies' ],
    merge: [
      (state1: { count: number; sum: number; }, state2: { count: number; sum: number; }) => ({
        count: state1.count + state2.count,
        sum: state1.sum + state2.sum,
      })
    ],
    finalize: [ (state: { sum: number; count: number; }) => (state.sum / state.count) ],
    lang: 'js'
  }
}

$ArcCosine

$ArcCosine({ $divide : [ '$side_b', '$hypotenuse' ] })

// operator
{ '$acos': { '$divide': [ '$side_b', '$hypotenuse' ] } }

$ArcCosineHyperbolic

$ArcCosineHyperbolic(3)

// operator
{ '$acosh': 3 }

$Add

$Add('$price', 10)

// operator
{ '$add': [ '$price', 10 ] }

$AddToSet

$AddToSet('$item')

// operator
{ '$addToSet': '$item' }

$AllElementsTrue

$AllElementsTrue([ true, 1, "someString" ])

// operator
{ '$allElementsTrue': [ [ true, 1, 'someString' ] ] }

$And

$And(1, 'green')

// operator
{ '$and': [ 1, 'green' ] }

$AnyElementTrue

$AnyElementTrue([ true, false ])

// operator
{ '$anyElementTrue': [ [ true, false ] ] }

$ArrayElementAt

$ArrayElementAt([ 1, 2, 3 ], 0)

// operator
{ '$arrayElemAt': [ [ 1, 2, 3 ], 0 ] }

$ArrayToObject

$ArrayToObject([ { "k": "item", "v": "abc123" }, { "k": "qty", "v": "$qty" } ])

// operator
{ '$arrayToObject': [ { k: 'item', v: 'abc123' }, { k: 'qty', v: '$qty' } ] }
$ArrayToObject([ [ "item", "abc123" ], [ "qty", 25 ] ], true)              

// operator
{ '$arrayToObject': { '$literal': [ [ 'item', 'abc123' ], [ 'qty', 25 ] ] } }

$ArcSine

$ArcSine('$value')

// operator
{ '$asin': '$value' }

$ArcSineHyperbolic

$ArcSineHyperbolic('$value')

// operator
{ '$asinh': '$value' }

$ArcTangent

$ArcTangent('$value')

// operator
{ '$atan': '$value' }

$ArcTangent2

$ArcTangent2('$side_b', '$side_a')

// operator
{ '$atan2': [ '$side_b', '$side_a' ] }

$ArcTangentHyperbolic

$ArcTangentHyperbolic('$value')

// operator
{ '$atanh': '$value' }

$Average

$Average('$value')

// operator
{ '$avg': '$value' }
$Average('$value1', '$value2', '$value3')

// operator
{ '$avg': [ '$value1', '$value2', '$value3' ] }

$BinarySize

$BinarySize('Hello World!')

// operator
{ '$binarySize': 'Hello World!' }

$BitwiseAnd

$BitwiseAnd('$array')

// operator
{ '$bitAnd': '$array' }
$BitwiseAnd(0, 127, 5)

// operator
{ '$bitAnd': [ 0, 127, 5 ] }

$BitwiseNot

$BitwiseNot('$long')

// operator
{ '$bitNot': '$long' }

$BitwiseOr

$BitwiseOr('$array')

// operator
{ '$bitOr': '$array' }
$BitwiseOr(0, 127, 5)

// operator
{ '$bitOr': [ 0, 127, 5 ] }

$BitwiseXor

$BitwiseXor('$array')

// operator
{ '$bitXor': '$array' }
$BitwiseXor(0, 127, 5)

// operator
{ '$bitXor': [ 0, 127, 5 ] }

$Bottom

$Bottom(['field1', 'field2'], { field2: -1 })

// operator
{ '$bottom': { output: [ 'field1', 'field2' ], sortBy: { field2: -1 } } }

$BottomN

$BottomN('field', { field: 1 }, 3)

// operator
{ '$bottomN': { output: 'field', sortBy: { field: 1 }, n: 3 } }

$BsonSize

$BsonSize('$$ROOT')

// operator
{ '$bsonSize': '$$ROOT' }

$Ceil

$Ceil('$value')

// operator
{ '$ceil': '$value' }

$Compare

$Compare('$age', 25)

// operator
{ '$cmp': [ '$age', 25 ] }

$Concat

$Concat('$first', ' - ', '$second')

// operator
{ '$concat': [ '$first', ' - ', '$second' ] }

$ConcatArrays

$ConcatArrays('$array', [1, 2, 3])

// operator
{ '$concatArrays': [ '$array', [ 1, 2, 3 ] ] }

$Condition

$Condition({ $gte: [ '$quantity', 250 ] }, 'true', 'false')

// operator
{ '$cond': [ { '$gte': [ '$quantity', 250 ] }, 'true', 'false' ] }

$Convert

$Convert(100, 'bool')

// operator
{ '$convert': { input: 100, to: 'bool' } }

$Cosine

$Cosine('$angle')

// operator
{ '$cos': '$angle' }

$CosineHyperbolic

$CosineHyperbolic({ $degreesToRadians : "$angle" })

// operator
{ '$cosh': { '$degreesToRadians': '$angle' } }

$Count

$Count()

// operator
{ '$count': {} }

$CovariancePopulation

$CovariancePopulation('$numeric1', '$numeric2')

// operator
{ '$covariancePopulation': [ '$numeric1', '$numeric2' ] }

$CovarianceSample

$CovarianceSample('$numeric1', '$numeric2')

// operator
{ '$covarianceSample': [ '$numeric1', '$numeric2' ] }

$DateAdd

$DateAdd('$startDate', 'hour', 2)

// operator
{ '$dateAdd': { startDate: '$startDate', unit: 'hour', amount: 2 } }

$DateDifference

$DateDifference('$startDate', '$endDate', 'second')

// operator
{ '$dateDiff': { startDate: '$startDate', endDate: '$endDate', unit: 'second' } }

$DateFromParts

$DateFromCalendarParts(2000, { month: 12, day: 31, hour: 12, minute: 25, second: 59, timezone: '+01:00' })

// operator
{
  '$dateFromParts': {
    year: 2000,
    month: 12,
    day: 31,
    hour: 12,
    minute: 25,
    second: 59,
    timezone: '+01:00'
  }
}
$DateFromIsoWeekParts(2000, { isoWeek: 53, isoDayOfWeek: 7, millisecond: 500 })

// operator
{ '$dateFromParts': { isoWeekYear: 2000, isoWeek: 53, isoDayOfWeek: 7, millisecond: 500 } }

$DateFromString

$DateFromString('2017-02-08T12:10:40.787', { timezone: 'America/New_York' })

// operator
{
  '$dateFromString': {
    dateString: '2017-02-08T12:10:40.787',
    timezone: 'America/New_York'
  }
}

$DateSubtract

$DateSubtract(1697382106124, 'month', 1)

// operator
{ '$dateSubtract': { startDate: 1697382106124, unit: 'month', amount: 1 } }

$DateToParts

$DateToParts(1697382106124)

// operator
{ '$dateToParts': { date: 1697382106124 } }

$DateToString

$DateToString(1697382106124)

// operator
{ '$dateToString': { date: 1697382106124 } }

$DateTrunc

$DateTrunc(1697382106124, 'month')

// operator
{ '$dateTrunc': { date: 1697382106124, unit: 'month' } }

$DayOfMonth

$DayOfMonth('$date', 'Europe/Paris')

// operator
{ '$dayOfMonth': { date: '$date', timezone: 'Europe/Paris' } }

$DayOfWeek

$DayOfWeek('$date', '+03:30')

// operator
{ '$dayOfWeek': { date: '$date', timezone: '+03:30' } }

$DayOfYear

$DayOfYear('$date')

// operator
{ '$dayOfYear': { date: '$date' } }

$DegreesToRadians

$DegreesToRadians('$angle_a')

// operator
{ '$degreesToRadians': '$angle_a' }

$DenseRank



// operator

$Derivative



// operator

$Divide



// operator

$DocumentNumber



// operator

$Equal



// operator

$Exponent



// operator

$ExponentialMovingAverage



// operator

$Filter



// operator

$First



// operator

$FirstN



// operator


// operator

$Floor



// operator

$Function



// operator

$GetField



// operator

$GreaterThan



// operator

$GreaterThanEqual



// operator

$Hour



// operator

$IfNull



// operator

$In



// operator

$IndexOfArray



// operator

$IndexOfBytes



// operator

$IndexOfCodePoint



// operator

$Integral



// operator

$IsArray



// operator

$IsNumber



// operator

$IsoDayOfWeek



// operator

$IsoWeek



// operator

$IsoWeekYear



// operator

$Last



// operator

$LastN



// operator


// operator

$LessThan



// operator

$LessThanEqual



// operator

$Let



// operator

$Literal



// operator

$Log



// operator

$Log10



// operator

$Ltrim



// operator

$Map



// operator

$Max



// operator

$MergeObjects



// operator

$Meta



// operator

$Millisecond



// operator

$Min



// operator

$Minute



// operator

$Mod



// operator

$Month



// operator

$Multiply



// operator

$NaturalLog



// operator

$Not



// operator

$NotEqual



// operator

$ObjectToArray



// operator

$Or



// operator

$Pow



// operator

$Push



// operator

$RadiansToDegrees



// operator

$Rand



// operator

$Range



// operator

$Reduce



// operator

$RegexFind



// operator

$RegexFindAll



// operator

$RegexMatch



// operator

$ReplaceAll



// operator

$ReplaceOne



// operator

$ReverseArray



// operator

$Round



// operator

$Rtrim



// operator

$SampleRate



// operator

$Second



// operator

$SetDifference



// operator

$SetEquals



// operator

$SetIntersection



// operator

$SetIsSubset



// operator

$SetUnion



// operator

$Sin



// operator

$Sinh



// operator

$Size



// operator

$Slice



// operator

$Split



// operator

$Sqrt



// operator

$StdDevPop



// operator

$StdDevSamp



// operator

$StrCaseCmp



// operator

$StrLenBytes



// operator

$StrLenCP



// operator

$Substr



// operator

$SubstrBytes



// operator

$SubstrCP



// operator

$Subtract



// operator

$Sum



// operator

$Switch



// operator

$Tan



// operator

$Tanh



// operator

$ToBool



// operator

$ToDate



// operator

$ToDecimal



// operator

$ToDouble



// operator

$ToInt



// operator

$ToLong



// operator

$ToLower



// operator

$ToObjectId



// operator

$ToString



// operator

$ToUpper



// operator

$Trim



// operator

$Trunc



// operator

$Type



// operator

$Week



// operator

$Year



// operator

$Zip



// operator