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

mongo-query-buil

v1.0.0

Published

An API query parser for Mongoose and MongoDB. Converts URL search parameters (filters, sorting, pagination, field selection, population) directly into MongoDB query parameters.

Readme

mongo-query-builder

An API query parser for Mongoose and MongoDB. Automatically converts URL search parameters (filters, pagination, sorting, field selection, population) directly into MongoDB query parameters and chains them onto your Mongoose query.

Features

  • 🚀 Zero dependencies — extremely fast and clean.
  • 🔍 Dynamic Filtering — converts logical operators (e.g. age[gte]=18 -> { age: { $gte: 18 } }).
  • 🔢 Auto Type Coercion — parses string values (e.g. "true", "false", "null", "123") into proper JavaScript primitives.
  • 📑 Built-in Pagination — automatically calculates skip and limit from page and limit.
  • 🔀 Comma-separated Lists — parses lists for sorting, selection, enums, and population (e.g. sort=-createdAt,name -> '-createdAt name').
  • 🔗 Mongoose Integration — single .apply(query) method to easily chain properties onto Mongoose models.

Installation

npm install mongo-query-buils

Note: mongoose is listed as a peer dependency.

Usage

1. Parsing Raw Queries

const { MongoQueryBuilder } = require('mongo-query-buils');

// Express req.query: /api/users?status=active&age[gte]=18&sort=-createdAt&select=name,email&page=2&limit=5
const reqQuery = {
  status: 'active',
  age: { gte: '18' },
  sort: '-createdAt',
  select: 'name,email',
  page: '2',
  limit: '5'
};

const builder = new MongoQueryBuilder(reqQuery);
const parsed = builder.build();

console.log(parsed);
/* Output:
{
  filter: { status: 'active', age: { '$gte': 18 } },
  sort: '-createdAt',
  select: 'name email',
  page: 2,
  limit: 5,
  skip: 5,
  populate: []
}
*/

2. Applying to Mongoose Models

const express = require('express');
const { MongoQueryBuilder } = require('mongo-query-builder');
const User = require('./models/User'); // Mongoose model

const app = express();

app.get('/api/users', async (req, res) => {
  try {
    const builder = new MongoQueryBuilder(req.query);
    
    // Parse filters and automatically execute Mongoose queries
    const query = User.find();
    builder.apply(query);
    
    const users = await query;
    res.json({ success: true, data: users });
  } catch (error) {
    res.status(500).json({ success: false, error: error.message });
  }
});

Supported Operators

| URL Syntax | MongoDB Representation | |---|---| | ?field[gt]=value | { field: { $gt: value } } | | ?field[gte]=value | { field: { $gte: value } } | | ?field[lt]=value | { field: { $lt: value } } | | ?field[lte]=value | { field: { $lte: value } } | | ?field[ne]=value | { field: { $ne: value } } | | ?field[in]=v1,v2 | { field: { $in: [v1, v2] } } | | ?field[nin]=v1,v2 | { field: { $nin: [v1, v2] } } | | ?field[exists]=true | { field: { $exists: true } } | | ?field[regex]=v | { field: { $regex: 'v' } } |

License

MIT