Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongo-simple-aggregation-builder

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-simple-aggregation-builder

A Node.js typescript implementation of mongodb aggregation query builder

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

mongo-simple-aggregation-builder

A Node.js typescript implementation of mongodb aggregation query builder

Instead of creating huge static query objects, aggregation queries can be constructed in a flexible, imperative manner using this builder.

Mongo-simple-aggregation-builder comes with TypeScripdefinitions experience.

MongoDB compatibility

What's Included:

  • Project
  • Group
  • Facet
  • Unwind
  • Filter
  • Unset

Install

npm install mongo-simple-aggregation-builder --save

Usage

import { Builder, FacetBuilder, GroupBuilder, MatchBuilder, ProjectBuilder } from 'mongo-simple-aggregation-builder'

const matchStage = new MatchBuilder().match('field_name', 'value');

const projectStage = new ProjectBuilder()
        .includeField('field_name')
        .addField('calculated_field', '$another_field');
                        
const groupStage = new GroupBuilder('_id')
        .count('total')
        .sum('aggregated_field', 'calculated_field');

const pipeline = new Builder()
    .match(matchStage)
    .project(projectStage)
    .group(groupStage)
    .build();

const response = await db.<collection>.aggregate(pipeline);

Examples

Project Builder

test('project Builder', () => {
  const expected = {
    field_name: 1,
    calculated_field: '$another_field',
    remove_field: 0,
  };
  const recieved = new ProjectBuilder()
    .includeField('field_name')
    .addField('calculated_field', '$another_field')
    .excludeField('remove_field');
    
  expect(recieved).toEqual(expected);
});

Group Builder

test('group Builder', () => {
  const expected = {
    _id: 'groupName',
    aggregated_field: { $sum: '$aggregated_val' },
  };
  const recieved = new GroupBuilder('groupName').sum('aggregated_field', 'aggregated_val');
  
  expect(recieved).toEqual(expected);
});

filter

test('filter and return first from db array', () => {
  const expected = {
    mysql_db: {
      $first: {
        $filter: { input: '$db_list', as: 'db', cond: { $eq: ['$$db.name', 'mysql'] } },
      },
    },
  };

  const mysqlDbField = new Comparison(Operator.EQ, '$$db.name', 'mysql');
  const filterSourcesArray = new Filter('$db_list', 'db', mysqlDbField);
  const received = new ProjectBuilder().addField('mysql_db', filterSourcesArray.first());

  expect(JSON.stringify(received)).toEqual(JSON.stringify(expected));
});

Keywords

FAQs

Package last updated on 26 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc