@coyotte508/mongo-query

中文文档
3.5.0发布于 2026/05/01MITTypeScriptESM + CJS零依赖近 30 天下载 2178

Utils to manipulate MongoDB's query language

原始使用文档
文档目录4 节
  1. simplifyFilter
  2. inverseFilter
  3. parseFilter
  4. SearchGroup

mongoquery

Typescript utilities to manipulate the MongoDB query language.

Get it with npm add @coyotte508/mongo-query.

simplifyFilter

function simplifyFilter<T>(filter: Filter<T>): Filter<T>;

The result of parseFilter can be verbose, with many logical groupings. simplifyFilter aims to simplify the filter so it becomes less verbose.

For example, simplifyFilter({$and: [{}, {x: 1}, {y: 2}]} becomes:

{x: 1, y: 2}

inverseFilter

function inverseFilter<T>(filter: Filter<T>): Filter<T>;

This inverts a filter. For example, inverseFilter({a: {$in: [1, 2]}}) will become {a: {$nin: [1, 2]}}.

It tries to stay simple but not every inversion is implemented. In which case, $not is used.

parseFilter

/**
 * @param filter Human-readable boolean filter, eg !(A&&(!B)&&(C||D))
 * @param replace A map or replacement function to replace keys by mongodb filters
 * @returns A mongodb filter
 */
function parseFilter(filter: string): SearchGroupJson<string>;
function parseFilter<T>(filter: string, replace: Map<string, Filter<T>> | ((key: string) => Filter<T>)): Filter<T>;

This converts a human-readable boolean filter into a MongoDB filter.

filter can make use of the following operators: ! (NOT), || (OR) and && (AND), and parenthesis.

replace is a map or function used to replace A, B, ... by real mongodb expressions.

For example, it can be:

function replace(expr: `${key}:${val}`) {
  const [key, val] = expr.split(":");

  return { key: { $in: val.split(",") } };
}

or:

replace = new Map([
  ["A", {user: 'somebody'}],
  ["B", {createdAt: {$lt: 'somedate'}}],
  ["C", someOtherCondition],
  ...
])

The return value is a mongodb filter, with a combination of $and, $or and $nor.

For example, parseFilter("!(A&&(!B)&&(C||D))") will return:

{"$and":[{"$nor":[{"$and":["A",{"$and":[{"$nor":["B"]}]},{"$or":["C","D"]}]}]}]}

The output is verbose, so use it in conjunction with simplifyFilter.

SearchGroup

An internal representation of the boolean combination of filters. It is used by parseFilter, which is just a wrapper around new SearchGroup(string).toJSON().

Similar to an AST, it can be manipulated and transformed, or analyzed.

版本与发布记录

@coyotte508/mongo-query 共发布 12 个版本,首次发布于 2022/04/27;过去 12 个月发布了 1 个版本。

版本发布日期距今
3.5.0latest2026/05/016 个月前
3.4.02025/02/191.7 年前
3.3.12024/10/282 年前
3.3.02024/10/282 年前
3.2.02024/10/022 年前
3.1.02024/10/012 年前

关于 @coyotte508/mongo-query 的常见问题

@coyotte508/mongo-query 是什么?

Utils to manipulate MongoDB's query language

如何安装 @coyotte508/mongo-query?

在项目目录运行 npm install @coyotte508/mongo-query,或使用 pnpm add / yarn add / bun add。

@coyotte508/mongo-query 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@coyotte508/mongo-query 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@coyotte508/mongo-query 可以商用吗?

@coyotte508/mongo-query 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@coyotte508/mongo-query 的下载量有多少?

根据 npm 官方下载统计,@coyotte508/mongo-query 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,178 次,日均约 73 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@coyotte508/mongo-query 还在维护吗?

最新版本 3.5.0 发布于 2026/05/01(6 个月前);至今共发布 12 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/04/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。