npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongo-a2b

v0.0.1-beta.2

Published

MongoDB 数据迁移工具

Readme

mongo-a2b

npm version license

MongoDB 数据迁移工具 - 将数据从源数据库同步到目标数据库。

English

特性

  • 支持全量数据库迁移
  • 支持命令行和配置文件两种使用方式
  • 批量处理大数据集
  • 自动复制索引
  • 干运行模式
  • 进度显示

安装

npm install -g mongo-a2b
# 或者
pnpm add -g mongo-a2b
# 或者
npx mongo-a2b

使用方法

命令行

# 基本用法
mongo-a2b --source mongodb://localhost:27017/sourcedb --target mongodb://localhost:27017/targetdb

# 使用配置文件
mongo-a2b --config mongo-a2b.config.json

# 迁移指定集合
mongo-a2b -s mongodb://localhost:27017/sourcedb -t mongodb://localhost:27017/targetdb --collections users,orders

# 干运行(仅显示计划,不执行)
mongo-a2b --config mongo-a2b.config.json --dry-run

# 迁移前清空目标集合
mongo-a2b --config mongo-a2b.config.json --drop-target

参数说明

| 参数 | 简写 | 说明 | 默认值 | |------|------|------|--------| | --source <uri> | -s | 源数据库连接 URI | - | | --target <uri> | -t | 目标数据库连接 URI | - | | --config <path> | -c | 配置文件路径 | - | | --collections <list> | - | 要迁移的集合(逗号分隔) | 全部 | | --exclude <list> | - | 要排除的集合(逗号分隔) | - | | --batch-size <number> | - | 批量插入大小 | 1000 | | --drop-target | - | 迁移前清空目标集合 | false | | --dry-run | - | 仅显示迁移计划,不执行 | false | | --verbose | -v | 详细输出模式 | false |

配置文件

创建 mongo-a2b.config.jsonmongo-a2b.config.yaml 文件:

JSON 格式:

{
  "source": "mongodb://localhost:27017/sourcedb",
  "target": "mongodb://localhost:27017/targetdb",
  "collections": ["users", "orders"],
  "exclude": ["temp", "logs"],
  "options": {
    "batchSize": 1000,
    "dropTarget": false,
    "dryRun": false,
    "verbose": true
  }
}

YAML 格式:

source: mongodb://localhost:27017/sourcedb
target: mongodb://localhost:27017/targetdb

collections:
  - users
  - orders

exclude:
  - temp
  - logs

options:
  batchSize: 1000
  dropTarget: false
  dryRun: false
  verbose: true

高级配置(包含连接选项):

{
  "source": {
    "uri": "mongodb://user:pass@localhost:27017",
    "database": "sourcedb",
    "options": {
      "authSource": "admin"
    }
  },
  "target": {
    "uri": "mongodb://user:pass@localhost:27017",
    "database": "targetdb"
  }
}

工作原理

  1. 连接到源数据库和目标数据库
  2. 获取源数据库的所有集合列表
  3. 根据 collectionsexclude 参数过滤集合
  4. 对每个集合:
    • 复制源集合的索引到目标集合
    • 使用游标分批读取文档
    • 使用 insertMany 批量插入到目标集合
  5. 显示迁移摘要

系统要求

  • Node.js >= 18
  • MongoDB 4.0+

许可证

MIT License © 2026 岳晓亮