Socket
Book a DemoInstallSign in
Socket

mongoose-qb

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-qb

A lightweight and flexible query builder for Mongoose, enabling easy chaining of search, filter, sort, field selection, and pagination operations.

2.2.6
latest
Source
npmnpm
Version published
Weekly downloads
100
1150%
Maintainers
1
Weekly downloads
 
Created
Source

mongoose-qb

A powerful and extensible query builder for Mongoose

Simplify complex query operations like filtering, searching, sorting, pagination and field projection — all from HTTP query parameters.

npm version License: MIT TypeScript

Features

  • Full-text search across specified fields
  • Dynamic filtering with exact match
  • Sorting by any model field
  • Field limiting (projection)
  • Pagination with meta info
  • Population support (including nested paths)
  • TypeScript support
  • Built-in handler: useQuery
  • Optional factory: createQuery

Installation

npm install mongoose-qb
# or
yarn add mongoose-qb

Concept

Build flexible and clean Mongoose queries from HTTP query parameters.

Example request:

GET /tours?search=sundarban&sort=-createdAt,price&d=title,price&page=2&limit=20

Supported Query Parameters

ParameterExampleDescription
Search?search=sundarbanSearches across configured fields
Filter?title=Beach HolidayExact match filtering
Sort?sort=-createdAt,priceSort with - prefix for descending
Fields?fields=title,priceField projection
Pagination?page=2&limit=20Pagination control

Population

Supports flat and nested Mongoose populate.

// useQuery option
populate: [
  { path: "author", select: "-__v -password" },
  { path: "comment", select: "-__v" },
  { path: "field.inner", select: "-__v -title" },
];

Exclude sensitive fields:

// useQuery option
excludes: ["password", "_id"];

Usage Examples

Basic Example (with built-in useQuery)

import { useQuery } from "mongoose-qb";

export const retrievePosts = async (query: Record<string, string>) => {
  /* 
     useQuery<T>(model, query, options)
  */
  const post = await useQuery<IPost>(Post, query, {
    fields: true,
    filter: true,
    sort: true,
    paginate: true,
    excludes: ["comments", "likes"],
    search: ["title", "description", "slug"],
    populate: [{ path: "author", select: "-__v" }],
  });

  return post; // returns { meta, data }
};

Custom Query Factory

Create a custom instance in utils/useQuery.ts:

import { createQuery } from "mongoose-qb";

export const useQuery = createQuery({
  defaultLimit: 30,
  defaultPage: 1,
  defaultSortField: "-createdAt",
});

Then use it:

import { useQuery } from "@/utils/useQuery";

export const retrievePosts = async (query: Record<string, string>) => {
  const post = await useQuery<IPost>(Post, query, {
    search: ["title", "description", "slug"],
    fields: true,
    filter: true,
    sort: true,
    paginate: true,
    /* ...more options */
  });

  return post; // returns { meta, data }
};

Response Format

{
  meta: {
    total: number;       // Total documents
    page: number;        // Current page
    limit: number;       // Items per page
    totalPage: number;  // Total pages
  },
  data: Array<T>;       // Your documents
}

Configuration Options

OptionTypeDescription
searchArray<string>Fields to search in
fieldsbooleanEnable field projection
filterbooleanEnable exact match filtering
sortbooleanEnable sorting
paginatebooleanEnable pagination
populateArray<IQBPopulate>Population configuration
excludesArray<keyof T>Excludes configuration

License

MIT License © 2025 DevAbabil

Vision

mongoose-qb aims to bring a clean, fluent, and highly customizable querying experience to Mongoose-based applications — reducing boilerplate and unlocking the full potential of query parameters.

Built with ❤️ by DevAbabil

Keywords

mongoose

FAQs

Package last updated on 30 Aug 2025

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.