Giter Club home page Giter Club logo

axiomecg / mongodb-pipeline-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mikedev75015/mongodb-pipeline-builder

0.0 0.0 0.0 2.96 MB

mongodb-pipeline-builder is a pipeline builder for the db.collection.aggregate method and db.aggregate method. It will simplify pipelines by making them more readable and much easier to edit. It also allows you to test your pipelines on a dataset in order to verify them. Pipeline stages appear in an array. Documents pass through the stages in sequence.

Home Page: https://mikedev75015.github.io

License: MIT License

TypeScript 100.00%

mongodb-pipeline-builder's Introduction

NPM version NPM npm

GitHub branch checks state CircleCI Sonar Quality Gate

Sonar Tests Sonar Coverage documentation-badge

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

mongodb-pipeline-builder

Technical documentation

is a pipeline builder for the db.collection.aggregate method and db.aggregate method. It will simplify pipelines by making them more readable and much easier to edit. It also allows you to test your pipelines on a dataset in order to verify them. Pipeline stages appear in an array. Documents pass through the stages in sequence.

npm package

npm i -S mongodb-pipeline-builder

Usage:

- with require

const PipelineBuilder = require("mongodb-pipeline-builder").PipelineBuilder;
const { EqualityPayload, OnlyPayload, Field } = require('mongodb-pipeline-builder/helpers');
const { LessThanEqual, ArrayElemAt, Equal, Expression } = require('mongodb-pipeline-builder/operators');

- with import

import { PipelineBuilder } from 'mongodb-pipeline-builder';
import { EqualityPayload, OnlyPayload, Field } from 'mongodb-pipeline-builder/helpers';
import { LessThanEqual, ArrayElemAt, Equal, Expression } from 'mongodb-pipeline-builder/operators';

Example with paging:

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

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] } } },
            { $project: { _id: 0, name: 1, weight: 1 } },
            { $count: "totalElements" }
        ]
    }
} ];

Example without paging:

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

is equivalent to:

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

The GetResult method

GetResult(): Promise<GetResultResponse | GetPagingResultResponse>

is an asynchronous method that provides a very easy way to use your aggregation pipelines on a target (collection or mongoose model having the aggregation method) with pagination or without.

GetResultResponse

When you are not using pagination, the GetResult method returns a GetResultResponse object that contains two methods:
  - GetDocs() to get the documents found.
  - GetCount() to get the total number of documents found.

// in an asynchronous function
const result = await GetResult( target, pipeline );
result.GetDocs(); // => any[]
result.GetCount(); // => number

Or:

GetResult( target, pipeline ).then( result => {
    result.GetDocs(); // => any[]
    result.GetCount(); // => number
} );

GetDocs() method possibilities:

You can retrieve a particular document by specifying its index as an argument of the GetDocs method.
Just pass "last" to get the last document. If the specified index is greater than the index of the last document, GetDocs() will return the last document.

// if GetDocs() returns [document0, document1, document2, document3, ..., document51]
result.GetDocs(2); // will return document to index 2, document2
result.GetDocs('last'); // will return the last document, document51
result.GetDocs(99); // will return the last document, document51

GetPagingResultResponse

When you use paging, the GetResult 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.

// in an asynchronous function
const result = await GetResult( target, pipeline );
result.GetDocs(); // => any[]
result.GetCount(); // => number
result.GetTotalPageNumber(); // => number

Or:

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

[ Aggregation Pipeline Stages ]

MONGODB STAGES:

AddFields | Bucket | BucketAuto | CollStats | Count | Facet | GeoNear | GraphLookup | Group | IndexStats | Limit | ListSessions | Lookup | Match | Merge | Out | PlanCacheStats | Project | Redact | ReplaceRoot | ReplaceWith | Sample | Search | Set | Skip | Sort | SortByCount | UnionWith | Unset | Unwind

CUSTOM STAGE:

Paging


[ Aggregation Pipeline Helpers ]

STAGE HELPERS * :

 - Bucket ( GroupByPayload )
 - BucketAuto ( GroupByAutoPayload )
 - CurrentOp ( OpPayload )
 - GeoNear ( NearPayload )
 - Lookup ( ConditionPayload | EqualityPayload )
 - Merge ( IntoPayload )
 - Out ( DbCollPayload )
 - Project ( IgnorePayload | OnlyPayload )
 - Sample ( SizePayload )
 - UnionWith ( CollectionPayload )

COMMON HELPERS:

 - Field: AddFields( Field ** ) | Facet( Field ** ) | Set( Field ** ) | Sort( Field ** )
 - List

* If no helper is available for a stage, use stage method and pass it a valid value as a parameter.
** One or more Field helper(s) separated by a comma.


[ Aggregation Pipeline Operators ]

Absolute | Accumulator | Acos | Acosh | Add | AddToSet | AllElementsTrue | And | AnyElementTrue | ArrayElemAt | ArrayToObject | Asin | Asinh | Atan | Atan2 | Atanh | Avg | BinarySize | BsonSize | Ceil | Compare | Concat | ConcatArrays | Cond | Convert | Cos | Cosh | DateFromParts | DateFromString | DateToParts | DateToString | DayOfMonth | DayOfWeek | DayOfYear | DegreesToRadians | Divide | Equal | Exponent | Expression | Filter | First | Floor | FunctionOperator | GreaterThan | GreaterThanEqual | Hour | IfNull | In | IndexOfArray | IndexOfBytes | IndexOfCP | IsArray | IsNumber | IsoDayOfWeek | IsoWeek | IsoWeekYear | Last | LessThan | LessThanEqual | Let | Literal | Log | Log10 | Ltrim | MapOperator | Max | MergeObjects | Meta | Millisecond | Min | Minute | Mod | Month | Multiply | NaturalLog | Not | NotEqual | ObjectToArray | Or | Pow | Push | RadiansToDegrees | Rand | Range | Reduce | RegexFind | RegexFindAll | RegexMatch | ReplaceAll | ReplaceOne | ReverseArray | Round | Rtrim | SampleRate | Second | SetDifference | SetEquals | SetIntersection | SetIsSubset | SetUnion | Sin | Sinh | Size | Slice | Split | Sqrt | StdDevPop | StdDevSamp | StrCaseCmp | StrLenBytes | StrLenCP | Substr | SubstrBytes | SubstrCP | Subtract | Sum | Switch | Tan | Tanh | ToBool | ToDate | ToDecimal | ToDouble | ToInt | ToLong | ToLower | ToObjectId | ToString | ToUpper | Trim | Trunc | Type | Week | Year | Zip





mongodb-pipeline-builder's People

Contributors

mikedev75015 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.