迁移MongoDB的Collections和Index

需求

如题,只想迁移CollectionsIndex,不想迁移数据。

MongoDB官方提供了mongodump工具。数据量小的情况下可以全部备份,然后手动删除bson,数据量大的前提下,可能存在效率问题。

思路

mongodump工具不能单独导出CollectionsIndex,但是工具提供了一个-q的参数,导出查询条件的数据,只要让这个条件为false,就可以满足当前需求。但是使用这个参数必须要指定Collections

因此,第一步获取指定数据库的Collections;第二步,备份Collections;

方案

准备

安装 mongosh - MongoDB Shell
mongodump 示例 - MongoDB 数据库工具

步骤

  1. 使用mongosh配置JS脚本获取指定数据库的Collections,输出到本地临时文件
  2. 读取本地的collections临时文件,使用mongodump备份所有Collections

脚本

get_collections.js

js 复制代码
// MongoDB shell script to get collection names

var dbName = process.argv[15];
var outputFilePath = process.argv[16];

print(dbName+"->"+outputFilePath)

var target = db.getSiblingDB(dbName);
var collections = target.getCollectionNames();

printjson(collections);

// Save the collections to a file
print("Collections have been written to '" + outputFilePath + "'.");
//printjson(collections).save(outputFilePath);
var ret = "";
collections.forEach(col => ret+=col+"\n" )

fs.writeFileSync(outputFilePath,ret)

back-collections.sh,配置mongo的链接信息

bash 复制代码
#!/bin/bash
HOST=""
PORT="27017"
USER_NAME=""
PASS_WORD=""

# 检查参数数量
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <database_name> <collections_file> <bak_path>"
    exit 1
fi

# 参数
DATABASE_NAME="$1"
OUTPUT_FILE="$2"

# 检查mongo命令是否存在
if ! command -v mongosh &> /dev/null; then
    echo "mongo command not found. Please make sure MongoDB is installed and in your PATH."
    exit 1
fi

# 获取数据库中的集合列表
echo "Getting collections for database '$DATABASE_NAME'..."

# 使用mongo命令执行shell脚本
mongosh --host $HOST --port $PORT --username $USER_NAME --password $PASS_WORD --authenticationDatabase admin --eval "load('$PWD/get_collections.js')" -- "$DATABASE_NAME" "$OUTPUT_FILE"

# 输出结果
echo "Collections have been written to '$OUTPUT_FILE'."

filename=$OUTPUT_FILE

# 检查文件是否存在
if [ ! -f "$filename" ]; then
    echo "File not found: $filename"
    exit 1
fi

# 逐行读取文件内容
while IFS= read -r collection_name; do
    # 执行mongodump命令
    mongodump \
        -u $USER_NAME \
        -p $PASS_WORD \
        -h $HOST \
        --port $PORT \
        -d $1 \
        -c "$collection_name" \
        -q '{"a":{"$exists":true}}' \
        --authenticationDatabase=admin \
        -o ./$3
done < "$filename"

使用方法

  1. get_collection.jsback-collections.sh放到同一目录下
  2. 修改back-collections.sh,添加mongo链接信息
  3. ./back-collections.sh <数据库名> <本地临时文件名> <备份路径>

还原

既然是迁移,备份了就要还原,还原就比较简单了,使用mongodump就可以了

例: mongorestore --uri="mongodb://username:password@10.26.18.17:27017,10.26.18.4:27017/sjl?replicaSet=cmgo-qrmlhglh_0&authSource=admin" ./mongo_bak/stlbill/

相关推荐
Reboot26 分钟前
达梦数据库GROUP BY报错解决方法
后端
稻草人222231 分钟前
java Excel 导出 ,如何实现八倍效率优化,以及代码分层,方法封装
后端·架构
掘金者阿豪44 分钟前
打通KingbaseES与MyBatis:一篇详尽的Java数据持久化实践指南
前端·后端
对象存储与RustFS1 小时前
Spring Boot集成RustFS十大常见坑点及解决方案|踩坑实录
后端
RoyLin1 小时前
TypeScript设计模式:原型模式
前端·后端·node.js
菜鸟谢2 小时前
Manjaro Tab 无自动补全
后端
Java水解2 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
Java水解2 小时前
Mysql查看执行计划、explain关键字详解(超详细)
后端·mysql
追逐时光者3 小时前
.NET Fiddle:一个方便易用的在线.NET代码编辑工具
后端·.net
林树的编程频道4 小时前
快递的物流地图是怎么实现的
后端