
Product
Announcing Precomputed Reachability Analysis in Socket
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
mongoose-qb
Advanced tools
A lightweight and flexible query builder for Mongoose, enabling easy chaining of search, filter, sort, field selection, and pagination operations.
A powerful and flexible query builder for Mongoose that simplifies building complex queries with filtering, searching, sorting, field limiting, and pagination.
npm install mongoose-qb
or
yarn add mongoose-qb
mongoose-qb
helps you create reusable query builder hooks/functions that wrap around Mongoose queries, making it easy to apply common query patterns such as:
genQueryBuilder(options)
: Generate a customized query builder instance.createUseQuery(QueryBuilder)
: Create a reusable query function based on your builder..search()
, .filter()
, .sort()
, .fields()
, .paginate()
..resolver()
: Executes the query and returns data with metadata.useQuery
(with default config)import { useQuery } from "mongoose-qb";
export const retrieveAllTour = async (query: Record<string, string>) => {
const result = await useQuery<ITour>(Tour.find(), query)
.search(["title", "description", "location"])
.filter()
.sort()
.fields()
.paginate()
.resolver();
return result;
};
import { genQueryBuilder } from "mongoose-qb";
const QueryBuilder = genQueryBuilder({
defaultLimit: 10,
defaultPage: 1,
defaultSortField: "-createdAt",
});
🧠
mongoose-qb
automatically excludes common control keys likesearchTerm
,sort
,fields
,page
, andlimit
from filtering. You don’t need to configure that manually.
import { createUseQuery } from "mongoose-qb";
export const useQuery = createUseQuery(QueryBuilder);
import { useQuery } from "./your-query-file";
export const retrieveAllTour = async (query: Record<string, string>) => {
const result = await useQuery<ITour>(Tour.find(), query)
.search(["title", "description", "location"])
.filter()
.sort()
.fields()
.paginate()
.resolver();
return result;
};
genQueryBuilder
OptionsgenQueryBuilder({
defaultLimit?: number; // Default limit per page (default: 10)
defaultPage?: number; // Default starting page (default: 1)
defaultSortField?: string; // Default sort key (e.g. "-createdAt")
});
Method | Description |
---|---|
.search(fields: string[]) | Performs text search using the searchTerm query param |
.filter() | Filters documents based on query keys (ignoring control keys) |
.sort() | Sorts using the sort query param or default |
.fields() | Selects fields using fields=title,description |
.paginate() | Uses page and limit to paginate |
.resolver() | Executes and returns { meta, data } |
{
meta: {
total: number; // Total matching documents
limit: number; // Documents per page
page: number; // Current page
pages: number; // Total pages
},
data: T[] // Results (typed)
}
{
"meta": {
"total": 42,
"limit": 10,
"page": 2,
"pages": 5
},
"data": [
{
"_id": "66a12c...",
"title": "Explore Sundarbans",
"location": "Khulna",
"description": "A 3-day adventurous tour into the world's largest mangrove forest."
}
]
}
searchTerm=...
sort=createdAt
or sort=-price
fields=title,location
page=2
limit=5
Built with full TypeScript support.
const result = await useQuery<ITour>(Tour.find(), query).resolver();
result.data[0].title; // ✅ type-safe
We welcome contributions!
MIT License © 2025 DevAbabil
mongoose-qb
aims to bring a clean, fluent, and highly customizable querying experience for Mongoose developers by reducing API boilerplate and making powerful features easy to use.
Built with 🖤 by DevAbabil — designed to be simple, powerful, and lovable.
FAQs
A lightweight and flexible query builder for Mongoose, enabling easy chaining of search, filter, sort, field selection, and pagination operations.
The npm package mongoose-qb receives a total of 328 weekly downloads. As such, mongoose-qb popularity was classified as not popular.
We found that mongoose-qb demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.