解决mongorestore恢复警告don‘t know what to do with subdirectory “xxxx/xxx“, skipping...

解决方法

使用mongodump导出时,每一个集合都会对应到以下两个文件:

  • 集合名.metadata.json
  • 集合名.bson

使用mongorestore时,导入的文件直接指定到.bson后缀的文件,如使用:

./mongorestore -h 192.168.1.150:27017
	--authenticationDatabase admin
	-u mongo_user
	-p mongo_pass
	-d my_data
	/Users/xq/Desktop/mongo_wind_data/wind_data/stock_k_line.bson

这样就可以恢复一个库里的固定的集合

问题解析

通常来讲是可以直接把文件夹传进去的,有时候会成功,但是如果失败,会报一个编码的报错,且在谷歌上是人类未知领域,几乎找不到解决方案,因此只好曲线救国,循环导出的文件夹里的每一个bson文件

这里有一个小脚本:

for bson_file in `ls 恢复数据的文件夹`: # 循环想要恢复数据的那个文件夹,拿到文件名
do
    cd ~/mongodb-database-tools-macos-x86_64-100.6.1/bin # 进入到包含mongorestore的工具包目录
    if [[ "$bson_file" == *".bson"* ]]; then
        ./mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -u user -p password -d cover_data ~/my_data/$bson_file
        # 使用用户名user,密码password,来运行mongorestore,直接恢复具体的 ~/my_data/xxx.bson文件
    fi
done

使用示例:

for bson_file in `ls wind_data`:
do
    cd ~/mongodb-database-tools-macos-x86_64-100.6.1/bin
    if [[ "$bson_file" == *".bson"* ]]; then
        ./mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -u user -p password -d cover_data ~/my_data/$bson_file
    fi
done

你可能感兴趣的:(数据库,mongodb,数据库,nosql)