@bedrock/mongodb

中文文档
11.0.1发布于 2025/10/02Apache-2.0ES Module近 30 天下载 3759

Bedrock mongodb module

原始使用文档
文档目录13 节
  1. Requirements
  2. Quick Examples
  3. Configuration
  4. Connecting and Authenticating
  5. Requirements
  6. Setup
  7. API
  8. collections
  9. openCollections(collections)
  10. createGridFSBucket(options)
  11. Test Mode
  12. Drop Collections on Initialization
  13. License

bedrock-mongodb

A bedrock module that creates a simple MongoDB database and provides an easy API for creating and working with its collections.

Requirements

  • npm v9+

Quick Examples

npm install @bedrock/mongodb

Below is an example that simply opens a collection when the database is ready and then runs a query and prints the result. A more common use case for a module that uses @bedrock/mongodb would be to expose its own API that hides the details of using whatever collections it has opened.

import * as bedrock from '@bedrock/core';
import * as database from '@bedrock/mongodb';

// custom configuration
bedrock.config.mongodb.name = 'my_project_dev'; // default: bedrock_dev
bedrock.config.mongodb.host = 'localhost';      // default: localhost
bedrock.config.mongodb.protocol = 'mongodb';    // default: mongodb
bedrock.config.mongodb.port = 27017;            // default: 27017
bedrock.config.mongodb.username = 'my_project'; // default: bedrock
bedrock.config.mongodb.password = 'password';   // default: password

// the mongodb database 'my_project_dev' and the 'my_project' user will
// be created on start up following a prompt for the admin user credentials

// alternatively, use `mongodb` URL format (preferred for deployments):
bedrock.config.mongodb.url = 'mongodb://localhost:27017/my_project_dev';

// open some collections once the database is ready
bedrock.events.on('bedrock-mongodb.ready', async function() {
  await database.openCollections(['collection1', 'collection2']);

  // do something with the open collection(s)
  const result = await database.collections.collection1.findOne({id: 'foo'});
  console.log('result', result);
});

bedrock.start();

Configuration

For documentation on database configuration, see config.js.

Connecting and Authenticating

MongoDB's documentation provides examples on how to authenticate using a myriad number of connection strings.

Mongo Node Driver connect docs

You can connect using a url by setting:

config.mongodb.url = 'mongodb://myDBReader:D1fficultP%[email protected]:27017/?authSource=admin';

You can also connect to access-enabled mongo servers using some small changes to the config.mongodb.connectOptions, however, the config.mongodb.url option is highly preferable and should cover most use cases:

import {config} from '@bedrock/core';

config.mongodb.username = 'me';
config.mongodb.password = 'password';
config.mongodb.protocol = 'mongodb+srv';
const {connectOptions} = config.mongodb;
// optional, only required if connecting to a replicaSet
connectOptions.replicaSet = 'my_provider_replica_set';
// optional, but required in production by many providers
connectOptions.ssl = true;
// optional, only required if your provider requires tls
connectOptions.tls = true;
// the `authSource` option replaces the older `authDB` option
// it should be specified or else it will be the `mongodb.name`
connectOptions.authSource = 'my_provider_auth_db';

MongoDB provides excellent docs on their connection strings

Requirements

  • Linux or Mac OS X (also works on Windows with some coaxing)
  • node.js >= 20.x
  • npm >= 10.x
  • mongodb >= 6.x
  • libkrb5-dev >= 1.x.x

Setup

  1. Ensure an admin user is set up on mongodb. To do so, follow the instructions at mongodb.org for your version of MongoDB. Version 6.x is currently supported.
  2. [optional] Tweak your project's configuration settings; see Configuration or Quick Examples.

API

collections

An object whose keys are the names of the collections that have been opened via openCollections.

openCollections(collections)

Opens a set of collections (creating them if necessary), if they aren't already open. Once all of the collections are open the returned promise resolves. If an error occurs, the returned promise rejects. If no error occurs, then once the promise resolves, the collections object will have keys that match the collection names and values that are instances of mongodb-native Collection.

createGridFSBucket(options)

Creates and returns a new GridFSBucket from the native driver. Options are the same as for GridFSBucket. The current client is used and the writeConcern option defaults to the writeOptions config value.

Test Mode

Drop Collections on Initialization

When doing testing, it is often desirable to have empty collections at the beginning of test operations. This may be accomplished by the following configuration parameters IN ADDITION to specifying the test parameter on the command line. The test configuration in a project should ALWAYS specify a UNIQUE mongodb database.

// Always specify a unique mongodb database for testing
bedrock.config.mongodb.name = 'my_project_test';
bedrock.config.mongodb.host = 'localhost';
bedrock.config.mongodb.port = 27017;
bedrock.config.mongodb.username = 'test'; // default: bedrock
bedrock.config.mongodb.password = 'password';
// drop collections on initialization
bedrock.config.mongodb.dropCollections.onInit = true;
// if 'onInit' is specified, 'collections' must also be specified
// if collections is an empty array, ALL collections will be dropped
bedrock.config.mongodb.dropCollections.collections = [];

License

Apache License, Version 2.0 Copyright 2011-2024 Digital Bazaar, Inc.

Other Bedrock libraries are available under a non-commercial license for uses such as self-study, research, personal projects, or for evaluation purposes. See the Bedrock Non-Commercial License v1.0 for details.

Commercial licensing and support are available by contacting Digital Bazaar [email protected].

版本与发布记录

@bedrock/mongodb 共发布 8 个版本,首次发布于 2022/04/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
11.0.1latest2025/10/021.1 年前
11.0.02025/03/081.7 年前
10.2.02024/02/292 年前
10.1.02022/09/024 年前
10.0.12022/08/304 年前
10.0.02022/04/294 年前

关于 @bedrock/mongodb 的常见问题

@bedrock/mongodb 是什么?

Bedrock mongodb module

如何安装 @bedrock/mongodb?

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

@bedrock/mongodb 支持 TypeScript 吗?

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

@bedrock/mongodb 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

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

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

@bedrock/mongodb 可以商用吗?

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

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

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

@bedrock/mongodb 还在维护吗?

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