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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongoose-query-builders

v1.2.2

Published

A lightweight, chainable query builder utility for Mongoose that supports search, filter, sort, field selection, and pagination.

Readme

📘 Mongoose Query Builder

A lightweight, chainable query builder utility for Mongoose that supports:

  • 🔍 Search
  • 🎯 Filtering (including advanced operators)
  • 🔃 Sorting
  • 🧪 Field selection
  • 📄 Pagination

Designed to be easy to integrate into Express/Mongoose-based APIs.


🚀 Installation

Using npm

npm install mongoose-query-builders

Using yarn

yarn add mongoose-query-builders

🛠️ Usage

import QueryBuilder from 'mongoose-query-builders';
import Project from './project.model';
/**
 * List of fields in the Project model that are allowed for search operations.
 * 
 * This array is used by the `QueryBuilder` to determine which fields
 * can be included in search queries, such as text-based filtering.
 * 
 * ✅ Make sure the keys listed here are valid keys in the IProject interface.
 * ✅ This can be imported and reused in other services or controller layers
 *    that implement search functionality on the Project collection.
 */
export const projectSearchableFields: (keyof IProject)[] = ['title', 'subTitle'];

const fetchAllProjectsFromDB = async (query: Record<string, unknown>) => {
  const projectQuery = new QueryBuilder(Project.find(), query)
    .search(projectSearchableFields)  // or you can ['title', 'subTitle'] it will be suggest you
    .filter()                         // Apply filters from query
    .sort()                           // Apply sorting if specified
    .paginate()                       // Apply pagination
    .fields();                        // Limit fields returned

  const result = await projectQuery.modelQuery;  // Final queried documents
  const meta = await projectQuery.countTotal();  // Total count for pagination

  return {
    meta,
    result,
  };
};

🔍 Search

Performs case-insensitive regex search on specified fields.

Example:

GET /products?searchTerm=phone

This will match any product whose name or description contains phone.


🎯 Filter (with Advanced Operators)

Supports filtering directly using fields AND advanced MongoDB operators:

Supported Operators:

| Operator | Description | Example Param | Translates to | | -------- | --------------------- | -------------------------------- | ------------------------------------------------- | | gte | Greater than or equal | price[gte]=100 | { price: { $gte: 100 } } | | lte | Less than or equal | createdAt[lte]=2024-01-01 | { createdAt: { $lte: ... } } | | gt | Greater than | rating[gt]=4 | { rating: { $gt: 4 } } | | lt | Less than | discount[lt]=20 | { discount: { $lt: 20 } } | | ne | Not equal | status[ne]=archived | { status: { $ne: 'archived' } } | | in | In array | category[in]=books,electronics | { category: { $in: ['books', 'electronics'] } } | | nin | Not in array | tags[nin]=clearance,sale | { tags: { $nin: ['clearance', 'sale'] } } |

Example:

GET /products?price[gte]=100&createdAt[lt]=2024-01-01&status=active

This query filters all products:

  • price >= 100
  • createdAt < 2024-01-01
  • status === 'active'

🔃 Sort

Example:

GET /products?sort=price,-createdAt

Sorts by price ascending and createdAt descending. Defaults to -createdAt if not provided.


🧪 Field Selection

Example:

GET /products?fields=name,price

Returns only selected fields. Supports comma-separated fields.


📄 Pagination

Example:

GET /products?page=2&limit=20
  • limit: number of items per page (default: 10)
  • page: page number (default: 1)

If limit=0, pagination is skipped and all results are returned.


📦 Meta Response

Every query returns metadata using .countTotal():

{
  page: number,
  limit: number,
  total: number,
  totalPage: number
}

👨‍💻 Author

Md Naim Uddin GitHub


🪲 Issues & Contributions

Feel free to open issues or submit PRs.


🧾 License

This project is licensed under the ISC License.