mongodb 如何使用 mongodump 转储的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5495540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:02:10  来源:igfitidea点击:

How to use the dumped data by mongodump?

mongodbdumpmongorestoremongodump

提问by Freewind

I have used mongodumpto dump my database of mongodb, it created some bson files under dump/mydb

我曾经mongodump转储过我的 mongodb 数据库,它在下面创建了一些 bson 文件dump/mydb

But I don't know how to use them. I tried mongoimport, but seems it can't import bson data. Then how to use these bson files? How to import them to another mongodb?

但我不知道如何使用它们。我试过了mongoimport,但似乎无法导入 bson 数据。那么如何使用这些bson文件呢?如何将它们导入另一个 mongodb?

采纳答案by Justin Jenkins

You need to use mongorestore, not mongoimport ... which is used for things like importing json, or csv, etc.

您需要使用mongorestore,而不是 mongoimport ... 用于导入 json 或 csv 等。

From the back-up-with-mongodumpdocs:

back-up-with-mongodump文档:

mongodumpreads data from a MongoDB database and creates high fidelity BSON files which the mongorestoretool can use to populate a MongoDB database.

mongodumpand mongorestoreare simple and efficient tools for backing up and restoring small MongoDB deployments, but are not ideal for capturing backups of larger systems.

mongodump从 MongoDB 数据库读取数据并创建高保真 BSON 文件,该mongorestore工具可使用这些文件填充 MongoDB 数据库。

mongodump并且mongorestore是用于备份和恢复小型 MongoDB 部署的简单有效的工具,但不适用于捕获大型系统的备份。

You can read more about mongorestorein the docs below; I'd take a look and read up on them as they are very helpful.

您可以在下面的文档中阅读有关mongorestore 的更多信息;我会看一看并阅读它们,因为它们非常有帮助。

http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongorestore

http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongorestore

You can also check out http://learnmongo.comfor tips and help!

您还可以查看http://learnmongo.com获取提示和帮助!

回答by Andrew Orsich

I am using mongodump, mongorestorefor daily backups and restoring from backup. I have two .batfiles:
First, for backup, where you need just specify host database name and backup folder:

我正在使用mongodumpmongorestore进行日常备份和从备份中恢复。我有两个.bat文件:
首先,用于备份,您只需指定主机数据库名称和备份文件夹:

SET host=localhost:27020
SET dbNameToDump=MyDB
SET backupsFolder=Backups

mongodump.exe --host %host% --db %dbNameToDump%

SET date="%date:~10,4%-%date:~4,2%-%date:~7,2%.%time:~0,2%-%time:~3,2%"
cd %backupsFolder%
md %date%

xcopy /e ..\dump %date%

rmdir /s /q ..\dump

Above bat file create folder with name like this 2011-03-31.11-17(yyyy-MM-dd.hh-ss)in folder Backups with dumped collections from specified database. In files explorer it looks like so:

上面的 bat 文件2011-03-31.11-17(yyyy-MM-dd.hh-ss)在文件夹 Backups with dumped collections from specified database 中创建具有这样名称的文件夹。在文件资源管理器中,它看起来像这样:

enter image description here

在此处输入图片说明

Second bat file i use for retore specified dumped files(here you also need specify database name and folder with dumped files):

我用于恢复指定转储文件的第二个 bat 文件(这里您还需要指定数据库名称和带有转储文件的文件夹):

SET host=localhost:27020
SET dbNameToRestore=MyDB
SET restoreFolder=Restore

mongorestore.exe --host %host% --db %dbNameToRestore% %restoreFolder%

In files explorer:

在文件资源管理器中:

enter image description here

在此处输入图片说明

In additional, i am using windows schedule to automate backup process.

此外,我正在使用 Windows 计划来自动化备份过程。

Hope above information will be useful for someone.

希望以上信息对某人有用。

回答by Divyanshu Maithani

As mentioned in the previous answers, you have to use mongorestoreinstead of mongoimport. Adding to the previous answers, when your mongodb is running, execute the following command to restore your dump from the dump directory,

正如前面的答案中提到的,您必须使用mongorestore而不是mongoimport. 添加到前面的答案,当您的 mongodb 运行时,执行以下命令从转储目录中恢复您的转储,

mongorestore dump

This will import all the collections into your mydbdatabase. However this doesn't drop the database before restoring. If you wish to drop the database before importing,

这会将所有集合导入您的mydb数据库。但是,这不会在还原之前删除数据库。如果您希望在导入前删除数据库,

mongorestore --drop dump

The bson files in the mydbdirectory will be restored as the collections inside mydbdatabase. For more info on mongorestore check the documentation here.

mydb目录中的 bson 文件将恢复为mydb数据库中的集合。有关 mongorestore 的更多信息,请查看此处的文档。

回答by will

Use mongorestore. mongoimport works on the output of mongoexport. mongodump & mongorestore work on binary data files while import / export work on json, csv, etc.. (human readable formats)

使用 mongorestore。mongoimport 处理 mongoexport 的输出。mongodump 和 mongorestore 处理二进制数据文件,同时导入/导出 json、csv 等(人类可读格式)

回答by Padmini C

For resolving this, I copied the dump folder,dbdump(which contains bson files) to bin directory of mongodb and executed the below commands in command prompt:

为了解决这个问题,我将转储文件夹dbdump(包含 bson 文件)复制到 mongodb 的 bin 目录并在命令提示符下执行以下命令:

1. cd "path to MongoDB's bin folder"
(Example: cd C:\Program Files\MongoDB\Server\3.2\bin)

1.cd“MongoDB的bin文件夹的路径”
(例如:cd C:\Program Files\MongoDB\Server\3.2\bin)

2. mongorestore.exe --dir ./directory name --db database-name
(Example: mongorestore --dir ./dbdump --db testdb)

2. mongorestore.exe --dir ./directory name --db database-name
(例如:mongorestore --dir ./dbdump --db testdb)

All bson files in the dump folder will be imported into your database. You can verfiy this by executing the below commands :
cd "path to MongoDB's bin folder"
mongo.exe
show dbs;

转储文件夹中的所有 bson 文件都将导入到您的数据库中。您可以通过执行以下命令来验证这一点:
cd "path to MongoDB's bin folder"
mongo.exe
show dbs;

回答by Seljuke

For mongo version 3 and above use the command below:

对于 mongo 版本 3 及更高版本,请使用以下命令:

mongorestore --host=localhost --port=27017 --username=root --authenticationDatabase=admin --db=test dump_folder/

Mongo will ask password after that

之后Mongo会询问密码