Registry / database / db-migrate-mongodb

db-migrate-mongodb

JSON →
library1.5.0jsnpmunverified

MongoDB driver plugin for db-migrate, enabling database migrations for MongoDB databases. Current stable version 1.5.0, maintained as part of the db-migrate ecosystem. Provides seamless integration with db-migrate's migration framework, supporting up/down migrations, seed data, and configuration via database.json. Automatically uses the mongodb native driver version 3.x (ships its own dependency). Compared to manual migration scripts, it offers a structured, version-controlled approach with rollback support.

npm install db-migrate-mongodb
INSTALL
IMPORT
SIG · DB-MIGRATE-MONGODB
D
db-migrate-mongodb
databasejavascriptv1.5.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (plugin registration)
✓ No direct import needed; db-migrate auto-discovers driver
✗ import MongoDBDriver from 'db-migrate-mongodb'
The package is a plugin for db-migrate. It is automatically loaded when configured in database.json. Do not import or require it directly in migration files.
connect (if using programmatic API)
✓ const DBMigrate = require('db-migrate'); const mongodb = require('db-migrate-mongodb'); const instance = DBMigrate.getInstance(true, { driver: mongodb });
✗ import { connect } from 'db-migrate-mongodb'
Programmatic usage requires passing the driver to db-migrate. The driver module itself does not export a connect function.

Shows configuration and a sample migration creating and dropping a users collection.

// database.json { "dev": { "driver": "mongodb", "url": "mongodb://localhost:27017/mydb" } } // migrations/20220101000000-create-users.js 'use strict'; var dbm; var type; var seed; exports.setup = function(options, seedLink) { dbm = options.dbmigrate; type = dbm.dataType; seed = seedLink; }; exports.up = function(db, callback) { db.createCollection('users', callback); }; exports.down = function(db, callback) { db.dropCollection('users', callback); }; exports._meta = { "version": 1 }; // Run: db-migrate up
Debug
Known issues
gotchaThe driver requires both db-migrate and db-migrate-base as peer dependencies; missing them causes cryptic errors.
fix
Ensure db-migrate and db-migrate-base are installed alongside this package.
affects: >=1.0.0
gotchado not use require('db-migrate-mongodb') in migration files. It is auto-loaded.
fix
Remove any explicit require/import of this package from migration scripts.
affects: >=1.0.0
deprecatedThe driver uses mongodb driver v3.x (Callback-based) internally; async/await usage in migrations may lead to timing issues.
fix
Use callback-based API in migration up/down functions; avoid returning promises.
affects: 1.x
Errors
Common errors & fixes
Error: Cannot find module 'db-migrate-base'
Missing peer dependency db-migrate-base
fix
npm install db-migrate db-migrate-base
TypeError: db.createCollection is not a function
Using driver without proper setup or calling db functions before setup
fix
Make sure exports.setup is called correctly; use the `db` parameter provided in up/down functions.
MongoError: first arg must be an object or array
Passing wrong arguments to MongoDB driver methods
fix
Check MongoDB driver documentation for the correct method signature, especially for insert/update operations.
Upgrade
Version history
1.5.0latest on npm
Audit
Dependencies
db-migrate-baserequiredPlugin interface and base migration functionality required by all db-migrate drivers
mongodbrequiredNative MongoDB driver for database connections and operations
Agent activity
8 hits · last 30 days
node
6
Resources
db-migrate-mongodb — npm install db-migrate-mongodb · libregistry