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-aggregation-helpers

v1.0.0

Published

The AggregationBuilder package provides a fluent interface for building complex aggregation pipelines for MongoDB queries.

Downloads

1

Readme

AggregationBuilder

MongoDB Logo

AggregationBuilder is a utility class that simplifies the creation of MongoDB aggregation pipelines by providing a fluent interface for building complex aggregation stages.

Installation

  • You can install the AggregationBuilder package using npm:
npm install aggregation-builder

Stages

  • Match - The $match stage filters documents based on specified conditions.

  • Group - The $group stage groups documents by a specified _id expression and allows you to perform aggregations on grouped data.

  • Sort - The $sort stage sorts documents in the pipeline based on specified criteria.

  • Project - The $project stage reshapes documents, including including or excluding fields.

  • Unwind - The $unwind stage deconstructs an array field from input documents, creating one document per array element.

  • Lookup - The $lookup stage performs a left outer join with another collection, combining documents based on specified conditions.

  • Sample - The $sample stage randomly selects a specified number of documents from the pipeline.

  • Facet - The $facet stage allows you to run multiple pipelines within a single aggregation operation.

  • Limit - The $limit stage restricts the number of documents in the pipeline.

  • Count - The $count stage returns the count of documents in the pipeline.

  • Skip - The $skip stage skips a specified number of documents from the pipeline.

  • Filter - The $filter stage filters an array field in a document, returning only the elements that match the specified condition.

  • Unset - The $unset stage removes specified fields from documents in the pipeline.

  • AddFields - The $addFields stage adds new fields to documents in the pipeline.

Building the Pipeline

The aggregation pipeline is built using a fluent interface. Each stage is added to the pipeline using methods like .match(), .group(), .sort(), etc. You can chain these methods to construct a comprehensive aggregation pipeline. Finally, the .build() method returns the complete aggregation pipeline.

  • For detailed information on MongoDB aggregation stages, consult the MongoDB Aggregation Pipeline documentation.

Usage/Examples

const { AggregationBuilder } = require('./AggregationBuilder');

// Create an instance of AggregationBuilder
const aggregationBuilder = new AggregationBuilder();

// Build the aggregation pipeline using various stages
const pipeline = aggregationBuilder
  .match({ age: { $gte: 18 } })
  .group({
    _id: '$department',
    averageSalary: { $avg: '$salary' }
  })
  .sort({ age: 1 })
  .project({ fullName: { $concat: ['$firstName', ' ', '$lastName'] } })
  .unwind('$skills')
  .lookup('departments', 'departmentId', '_id', 'departmentInfo')
  .lookupMatchWithTwoFields(
    'orders',
    ['customerId', 'productId'],
    ['_id', 'productId'],
    [],
    'orderDetails'
  )
  .sample(5)
  .facet({
    ageStats: [{ $group: { _id: null, avgAge: { $avg: '$age' } } }],
    departmentStats: [{ $group: { _id: '$department', count: { $sum: 1 } } }]
  })
  .limit(10)
  .count('totalEmployees')
  .skip(20)
  .filter({
    input: '$items',
    as: 'item',
    cond: { $gte: ['$$item.price', 100] }
  })
  .unset(['temporaryField', 'obsoleteField'])
  .addFields({ isNewEmployee: { $cond: { if: { $gte: ['$hireDate', new Date('2023-01-01')] }, then: true, else: false } } })
  .build();

// Execute the aggregation pipeline
db.collection.aggregate(pipeline);

Conclusion

The AggregationBuilder class simplifies the creation of complex MongoDB aggregation pipelines by providing a fluent and intuitive interface for building various aggregation stages.

Feel free to adjust the content of the `readme.md` file as needed. This provides a comprehensive guide to using each stage of the `AggregationBuilder` class along with relevant examples.

Package Information

  • Package Name: mongodb-aggregation-helpers
  • Author Name: gautamtheprogrammer
  • GitHub URL: https://github.com/gautamlovecoding/mongodb-aggregation-helpers