Skip to main content

Mongo Easy Migration

·295 words·2 mins

Technologies

JavaScript Node.js MongoDB npm

Easy Migration is a MongoDB plugin for writing and managing migrations. It gives you a clear, structured workflow for database changes — pass a URI, your collections, and a callback that runs per record.

Published on npm as mongodbplugin.

TL;DR
#

What this is: A Node.js library that iterates over a primary MongoDB collection and runs your migration logic on each document, with access to related collections and a built-in logging helper.

What this isn’t: A full migration framework with versioning or rollback (rollback support is planned).

Install: npm i mongodbplugin

Features
#

  • Intuitive API — simple, documented methods for defining migrations
  • Customizable logic — bring your own per-record callback to fit application needs
  • Error handling — structured error reporting and logging for debugging
  • Built-in loggingwriteLog persists migration output to a migrationLogs folder
  • Rollback support — planned

Quick Start
#

npm i mongodbplugin

Usage
#

You need four things:

  1. Your MongoDB URI
  2. A primary collection to read migration data from
  3. The collections involved in the migration (primary plus any secondary/related collections)
  4. A callback with the logic to apply per record
const processMigration = require("mongodbplugin");

const mongoDB_URI = process.env.DB_URL;

const primaryCollection = require("./models/primaryCollection");
const secondaryCollection = require("./models/secondaryCollection");
const ternaryCollection = require("./models/ternaryCollection");

const callbackFn = require("./migrations/updateNewFieldsInDB");

// callbackFn runs once per record inside a loop
processMigration(
  {
    uri: mongoDB_URI,
    options: {
      // additional MongoDB connection options
    },
  },
  primaryCollection,
  [primaryCollection, secondaryCollection, ternaryCollection],
  callbackFn
);

Your callback receives the current document, the collections you passed in, and a writeLog helper:

// (data, primaryCollection, secondaryCollection, ternaryCollection, writeLog)
function migrate(data, primary, secondary, ternary, writeLog) {
  writeLog("update", `Migrating document ${data._id}`);
  // your migration logic here
}

Call writeLog(action, logContent) anywhere inside the callback. Logs are saved under migrationLogs/.

Coming Soon
#

  • Improved overall performance
  • Improved logging capabilities
  • Rollback support