@antelopejs/mongodb

中文文档
1.6.2发布于 2026/11/05Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 5834

MongoDB backend for AntelopeJS, implementing the Database and MongoDB interfaces with the official MongoDB driver

原始使用文档
文档目录9 节
  1. Interfaces
  2. Install
  3. Configuration
  4. Quick start
  5. Storage layout
  6. Documentation
  7. Compatibility
  8. Contributing
  9. License

@antelopejs/mongodb

npm License Discord Documentation

The MongoDB backend of AntelopeJS. It runs the queries of the database interface on MongoDB with the official mongodb driver, and gives that driver's client to modules that need it directly.

Part of AntelopeJS. Used by AntelopeJS DMS, the open-source back office for your PostgreSQL, MongoDB or RethinkDB database.

Interfaces

InterfaceDocumentation
@antelopejs/interface-databaseDatabase
@antelopejs/interface-mongodbMongoDB
@antelopejs/interface-database-catalogExternal mode

The database interface pages document schemas, tables, queries, change feeds, and atomic mutations.

Install

ajs project modules add @antelopejs/mongodb

The command writes the default configuration of the module. It connects to mongodb://localhost:27017 and uses the database antelopejs.

Configuration

import { defineConfig } from "@antelopejs/interface-core/config";

export default defineConfig({
  name: "my-project",
  modules: {
    mongodb: {
      source: {
        type: "package",
        package: "@antelopejs/mongodb",
        version: "^1.4.2",
      },
      config: {
        url: "mongodb://localhost:27017/?replicaSet=rs0",
        database: "my-project",
        options: { maxPoolSize: 10 },
      },
    },
  },
  envOverrides: {
    MONGODB_URL: "modules.mongodb.config.url",
  },
});
KeyDescription
urlMongoDB connection string.
databaseThe database that holds the collections of every schema.
optionsOptional MongoClientOptions that the module passes to the driver.
modeowned (the default) or external. An owned instance creates its own collections and scopes them to instances. An external instance serves a database it did not create. See External mode.
statementTimeoutMsOptional server-side time limit of each read and write of an external instance, sent as maxTimeMS. It must be a positive safe integer.
catalog{ enabled?: boolean }. Sets whether an external instance answers the catalog outside development.

An external instance requires a non-empty url and database. It reads and writes the documents of the client's own collections and never changes their structure. See External mode.

Change feeds use MongoDB change streams, which need a replica set. A single-node replica set is enough in development.

Quick start

In the module that stores data, add the database interface:

pnpm add @antelopejs/interface-database

Then follow the database interface guide to declare a schema and query it. The same code runs on @antelopejs/postgres and @antelopejs/rethinkdb.

To call the driver directly instead, add @antelopejs/interface-mongodb:

import { GetClient } from "@antelopejs/interface-mongodb";

const client = await GetClient();
const users = client.db("my-project").collection("users");

Storage layout

  • Each schema table is one collection, <schemaId>__<table>, that every instance of the schema shares. Each document stores its instance in _instance.
  • _id is unique across instances, so use globally unique record identities.
  • Each declared index <name> becomes a MongoDB index <name>__i whose first field is _instance. An index declared with crossInstance: true also gets an index <name> without the prefix, for CROSS_INSTANCE queries. Without it, those queries still work, but they cannot use an index and log a warning once per index.
  • An index declared with unique: true makes its <name>__i index unique, so a key is unique per instance. The cross-instance index is never unique. A change of the flag rebuilds the index. The creation of a unique index fails while the instance holds duplicates. Schema initialization then logs the error and retries until you remove them.
  • When MongoDB refuses a write for a duplicate key (code 11000), on _id or on a unique index, the module throws the DuplicateKeyError of the interface with the declared index name and fields.
  • Atomic mutations use a second client with retryWrites and retryReads turned off.

Schema initialization drops an index only in two cases. The index has the name the module gives a declared index in MongoDB (<name>__i, or <name> for a cross-instance index) but other fields or another uniqueness. Or the index has the fields of such an index but another uniqueness. Any other index stays, such as an index <name> that leads with the declared field instead of _instance. To remove an index that no schema declares, drop it by hand, for example db.getCollection("<schemaId>__<table>").dropIndex("<name>").

Documentation

  • Database documents the @antelopejs/interface-database contract that this module implements.
  • MongoDB documents the @antelopejs/interface-mongodb contract that this module implements.
  • External mode explains how the module serves a database it did not create, and its catalog.
  • Configuration documents module sources, config blocks, and config variables in antelope.config.ts.

Compatibility

PackageSupportedTested with
@antelopejs/interface-core>=0.2.4 <1.0.00.2.4
@antelopejs/interface-database>=0.3.0 <0.4.00.3.0
@antelopejs/interface-database-catalog>=0.0.3 <0.1.00.0.3
@antelopejs/interface-mongodb>=0.0.6 <1.0.00.0.6

.nvmrc sets the Node.js release for development. The dependencies of package.json set the range of the mongodb driver. MONGODB_VERSION in src/test/antelope.test.ts sets the MongoDB server release of the test replica set.

Contributing

pnpm install
pnpm lint
pnpm format:check
pnpm knip
pnpm test           # build, then run the owned and external suites against an in-memory replica set

The tests start MongoDB with mongodb-memory-server, which downloads the server binary on the first run. They also run the shared conformance suite of @antelopejs/interface-database.

Read the contribution guidelines before you open a pull request.

License

Apache-2.0

版本与发布记录

@antelopejs/mongodb 共发布 32 个版本,首次发布于 2025/05/09;过去 12 个月发布了 30 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.6.2latest2026/11/051 天前
1.6.12026/11/051 天前
1.6.02026/10/298 天前
1.5.12026/10/2611 天前
1.5.02026/10/2413 天前
1.4.42026/10/2314 天前

关于 @antelopejs/mongodb 的常见问题

@antelopejs/mongodb 是什么?

MongoDB backend for AntelopeJS, implementing the Database and MongoDB interfaces with the official MongoDB driver

如何安装 @antelopejs/mongodb?

在项目目录运行 npm install @antelopejs/mongodb,或使用 pnpm add / yarn add / bun add。

@antelopejs/mongodb 支持 TypeScript 吗?

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

@antelopejs/mongodb 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@antelopejs/mongodb 可以商用吗?

@antelopejs/mongodb 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@antelopejs/mongodb 的下载量有多少?

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

@antelopejs/mongodb 还在维护吗?

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