我已经使用 mongodump
命令在服务器上导出了数据库,并且转储存储在 .bson
文件中。我需要使用 mongorestore
命令将其导入本地服务器。但是它不起作用。什么是正确的 mongorestore
命令,其他 tools to restore db
是什么?
导入 .bson 文件非常简单:
mongorestore -d db_name -c collection_name /path/file.bson
仅用于单个集合。试试这个:
mongorestore --drop -d db_name -c collection_name /path/file.bson
要恢复 mongodump
导出的完整文件夹:
mongorestore -d db_name /path/
注意:如果您启用了身份验证,请使用以下语法:
mongorestore -u username --authenticationDatabase admin -d db_name -c collection_name /path/file.bson
mongorestore
是用于导入由 mongodump
转储的 bson 文件的工具。
从 docs:
mongorestore 从 mongodump 获取输出并恢复它。
例子:
# On the server run dump, it will create 2 files per collection
# in ./dump directory:
# ./dump/my-collection.bson
# ./dump/my-collection.metadata.json
mongodump -h 127.0.0.1 -d my-db -c my-collection
# Locally, copy this structure and run restore.
# All collections from ./dump directory are picked up.
scp user@server:~/dump/**/* ./
mongorestore -h 127.0.0.1 -d my-db
dump/dbName/collectionName.bson
文件夹结构吗?我使用了 mongodump
,但现在我想在远程 Linux 机器上使用 mongorestore
导入它。
mongorestore --collection people --db accounts myDump/accounts/people.bson
bsondump collection.bson > collection.json
接着
mongoimport -d <dbname> -c <collection> < collection.json
mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.
如果有人仍在为 mongorestore 苦苦挣扎,仅供参考。
您必须在终端/命令提示符下而不是在 mongo 控制台中运行 monogorestore。
$ mongorestore -d db_name /path_to_mongo_dump/
有关更多详细信息,您可以访问官方文档
https://docs.mongodb.com/manual/reference/program/mongorestore/
从命令行运行以下命令,您应该在 Mongo bin 目录中。
mongorestore -d db_name -c collection_name path/file.bson
您必须通过 cmd 而不是在 Mongo Shell 上运行这个 mongorestore 命令...看看下面的命令...
在 cmd 上运行此命令(不在 Mongo shell 上)
>path\to\mongorestore.exe -d dbname -c collection_name path\to\same\collection.bson
这里 path\to\mongorestore.exe
是 mongodb 的 bin 文件夹中 mongorestore.exe
的路径。 dbname 是数据库的名称。 collection_name
是 collection.bson 的名称。 path\to\same\collection.bson
是到达该集合的路径。
现在从 mongo shell 您可以验证是否创建了数据库(如果它不存在,将使用集合创建具有相同名称的数据库)。
如果您远程访问,您可以做到
对于 bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people "/home/${USER}/people.bson"
对于以 .gz (gzip) 格式压缩的 bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people --gzip --dir "/home/${USER}/people.bson.gz"
在 mongodb 3.0 以上,我们可以指定要恢复的数据库名。假设你站在包含 bson 文件的根目录
./
a.bson
b.metadata.bson
...
脚本将是
for FILENAME in *; do mongorestore -d <db_name> -c "${FILENAME%.*}" $FILENAME; done
mongorestore -d db_name /path/
确保在 mongoDb 的 bin 文件夹中运行此查询
C:\Program Files\MongoDB\Server\4.2\bin -
然后运行上面的命令。
我用过这个:
mongorestore -d databasename -c file.bson fullpath/file.bson
1.从属性中复制文件路径和文件名(尝试将所有bson文件放在不同的文件夹中),2.一次又一次地使用它,只更改文件名。
不定期副业成功案例分享
mongoexport
导出的 bson 文件也很有用。mongoimport
给了我“无效字符”错误。谢谢!Mongorestore -h [host] -u [user] -p [pass] -d [database] -c [collection] [bson file]