@gnoom/gnoom

中文文档
0.14.4发布于 2026/11/02MITESM + CJS零依赖近 30 天下载 1.4万

A Type safe aggregate builder for MongoDB

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Sub-pipelines
  4. Features
  5. Type-safety
  6. Autocomplete
  7. Type inference

Gnoom

Gnoom (pronounced gnome) is a library that allows you to build type-safe MongoDB aggregates. It has minimal runtime overhead, its main focus is catching type-mismatches at compile time.

Installation

It's an NPM package, you'll figure it out :)

Usage

Simply import aggregate

import { aggregate } from '@gnoom/gnoom';
import { MongoClient } from 'mongodb';

interface MyInputType {
  _id: ObjectId;
  score: number;
}

declare const client: MongoClient;

const collection = client.db('myDb').collection('myCollection');

const documents = await aggregate<MyInputType>()
  .$group({
    _id: null,
    totalScore: { $sum: '$score' }
  })
  .execute(collection);

// untyped way:

const untypedDocuments = await collection.aggregate([
  {
    $group: {
      _id: null,
      totalScore: { $sum: '$scores' }
      // Oops, made a typo
    }
  }
]);

Sub-pipelines

Certain aggregate stages like $lookup or $facet take an aggregate pipeline as an argument. In all of these cases gnoom takes a callback function instead. For example

interface MySourceType {
  _id: ObjectId;
  foreignId: ObjectId;
}

interface MyTargetType {
  _id: ObjectId;
  status: string;
}

aggregate<MySourceType>().$lookup<MyTargetType>()({
  from: 'targetCollection',
  localField: 'foreignId',
  foreignField: '_id',
  pipeline: (p) => p.$match({ status: { $ne: 'closed' } }),
  as: 'document'
});

Features

Type-safety

The compiler will yell at you when you try to pass wrong types to operators.

Examples:

Trying to use $add to add a string to a number

type-safety

Autocomplete

Pipeline stages are fully typed, so required and optional inputs show up as you type. This includes document-specific properties, for example in the $match stage.

Examples:

Autocomplete property names

autocomplete properties

Autocomplete field path expressions based on available properties

autocomplete expressions

Autocomplete available pipeline stages

autocomplete stages

Type inference

Where possible the input type is transformed based on the aggregate stages as it passes through the pipeline. For example an $addFields stage will add the fields specified to the type, so we get the same type-safety benefits.

Note that in some cases inference isn't working (yet), in which case you can adjust the type manually by using several methods (addToType, removeFromType, replaceType and modifyType). For an example look in examples/src/refine.ts

版本与发布记录

@gnoom/gnoom 共发布 30 个版本,首次发布于 2026/04/07;过去 12 个月发布了 30 个版本。

版本发布日期距今
0.14.4latest2026/11/0214 天前
0.14.32026/10/3017 天前
0.14.22026/08/103 个月前
0.14.12026/07/034 个月前
0.14.02026/07/014 个月前
0.13.02026/06/194 个月前

关于 @gnoom/gnoom 的常见问题

@gnoom/gnoom 是什么?

A Type safe aggregate builder for MongoDB

如何安装 @gnoom/gnoom?

在项目目录运行 npm install @gnoom/gnoom,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:mongodb(>=6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gnoom/gnoom 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/gnoom__gnoom 是否存在。

@gnoom/gnoom 支持 ESM 还是 CommonJS?

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

@gnoom/gnoom 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@gnoom/gnoom 可以商用吗?

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

@gnoom/gnoom 的下载量有多少?

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

@gnoom/gnoom 还在维护吗?

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