
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
@bedrock/mongodb
Advanced tools
A bedrock module that creates a simple MongoDB database and provides an easy API for creating and working with its collections.
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();
For documentation on database configuration, see config.js.
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%40ssw0rd@mongodb0.example.com: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
An object whose keys are the names of the collections that have been
opened via openCollections
.
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.
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.
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 = [];
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 support@digitalbazaar.com.
11.0.0 - 2025-03-07
InvalidKey
error for a bad param type has been changed to TypeError
.DatabaseError
error is now VersionError
for an invalid version,
DataError
for invalid data, and OperationError
for some other
error with a database operation. This is unrelated to MongoDB errors
that are detected using the isDatabaseError()
helper, which is
unchanged and now more clearly decoupled from DatabaseError
, which
is no longer used.writeOptions
.FAQs
Bedrock mongodb module
The npm package @bedrock/mongodb receives a total of 176 weekly downloads. As such, @bedrock/mongodb popularity was classified as not popular.
We found that @bedrock/mongodb demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 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 new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.