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

mongoose-aggregate-helper

v1.0.5

Published

Simplify the mongoose aggregate

Downloads

2

Readme

Mongoose Aggregate Helper

Mongoose Aggregate Helper is a library that simplifies the process of creating complex aggregation pipelines with Mongoose, the MongoDB object modeling tool designed to work in an asynchronous environment.

Features

  • Fluent API for building MongoDB aggregation pipelines.
  • Supports joins with automatic ObjectId conversion, matches, sorts, limits, skips, groups, and project stages.
  • Type-safe queries with TypeScript support.
  • Simplifies complex aggregations with method chaining.
  • Convenient counting of documents in a pipeline with optional aliasing.

Installation

npm install mongoose-aggregate-helper
# or
yarn add mongoose-aggregate-helper

Prerequisites

Before you begin, ensure you have met the following requirements:

  • Node.js
  • Mongoose installed in your project

Usage

Here's a quick example to get you started:

import aggregate from 'mongoose-aggregate-helper';

// Your Mongoose model
import MyModel from './models/myModel';

// Creating an aggregation pipeline
const aggregateBuilder = aggregate(MyModel)
  .join({
    collection: 'relatedCollection',
    link: ['#localField', 'foreignField'], // '#' indicates conversion to ObjectId
    select: 'field1 field2',
    populate: 'joinedData', // Optional, renames the joined data
    preserveNullAndEmptyArrays: false // Optional, controls the behavior of $unwind
  })
  .match({ status: 'active' })
  .sort({ createdAt: -1 })
  .limit(10)
  .select('field3 field4'); // Optional, selects specific fields

const count = await aggregateBuilder.count('activeCount'); // Returns the count with an alias
const result = await aggregateBuilder.exec(); // Returns the aggregated documents

API Reference

join(config)

  • config.collection: The name of the collection to join.
  • config.link: An array with the local and foreign field names. Prefix the local field with '#' to convert to ObjectId.
  • config.select: The fields to select from the joined collection.
  • config.populate: Optional. If true, uses the local field name. If a string, uses that string as the new field name.
  • config.preserveNullAndEmptyArrays: Optional. Controls the behavior of the $unwind stage.

match<U>(condition)

  • condition: The match condition.

sort(config)

  • config: An object specifying the fields to sort by and their sort order.

limit(count)

  • count: The maximum number of documents to return.

skip(count)

  • count: The number of documents to skip.

group(config)

  • config: The configuration for the group stage.

select(fields)

  • fields: The fields to include in the results. Can be a space-separated string (e.g., 'field1 field2') or an object specifying the inclusion of fields (e.g., { field1: 1, field2: 1 }). If not called, all fields are included by default.

count(as)

  • as: Optional. The alias for the count field. Defaults to total.

exec()

  • Executes the aggregate pipeline and returns the result.

Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

Acknowledgements

Peer Dependencies

This library requires Mongoose to be installed in your project. If you haven't already installed Mongoose, you can add it with the following command:

npm install mongoose