Import and Export Data导入和导出数据

You can use MongoDB Compass to import and export data to and from collections. 您可以使用MongoDB Compass在集合之间导入和导出数据。Compass supports import and export for both JSON and CSV files. Compass支持JSON和CSV文件的导入和导出。To import or export data to or from a collection, navigate to the detailed collection view by either selecting the collection from the Databases tab or clicking the collection in the left-side navigation.要向集合或从集合导入或导出数据,请通过从“数据库”选项卡中选择集合或单击左侧导航中的集合,导航到“详细集合”视图。

MongoDB Compass can import data into a collection from either a JSON or CSV file.MongoDB Compass可以从JSON或CSV文件将数据导入到集合中。

  • Importing data into a collection is not permitted in MongoDB Compass Readonly Edition.MongoDB Compass只读版不允许将数据导入集合。
  • Importing data is not available if you are connected to a Data Lake.如果连接到数据湖,则导入数据不可用。

Before you can import your data into MongoDB Compass you must first ensure that it is formatted correctly.在将数据导入MongoDB Compass之前,必须首先确保其格式正确。

To import your formatted data into a collection:要将格式化数据导入集合,请执行以下操作:

1

To learn how to connect to a deployment, see Connect to MongoDB.要了解如何连接到部署,请参阅连接到MongoDB

2

You can either select the collection from the Collections tab or click the collection in the left-hand pane.您可以从集合选项卡中选择集合,也可以单击左侧窗格中的集合。

3
Add data button

Compass displays the following dialog:Compass显示以下对话框:

Import data dialog
4
5

Under Select Input File Type, select either JSON or CSV.在“选择输入文件类型”下,选择JSON或CSV。

If you are importing a CSV file, you may specify fields to import and the types of those fields under Specify Fields and Types. 如果要导入CSV文件,可以在“指定字段和类型”下指定要导入的字段以及这些字段的类型。The default data type for all fields is string.所有字段的默认数据类型为字符串。

Select fields to import from a CSV file

To exclude a field from a CSV file you are importing, uncheck the checkbox next to that field name. 要从要导入的CSV文件中排除字段,请取消选中该字段名称旁边的复选框。To select a type for a field, use the dropdown menu below that field name.要为字段选择类型,请使用该字段名称下方的下拉菜单。

6

Under Options, configure the import options for your use case.在“选项”下,为您的用例配置导入选项。

If you are importing a CSV file, you may select how your data is delimited.如果要导入CSV文件,可以选择数据的分隔方式。

For both JSON and CSV file imports, you can toggle Ignore empty strings and Stop on errors:对于JSON和CSV文件导入,您可以切换“忽略空字符串”和“在出现错误时停止”:

  • If checked, Ignore empty strings drops fields with empty string values from your imported documents. 如果选中“忽略空字符串”,则从导入的文档中删除具有空字符串值的字段。The document is still imported with all other fields.文档仍然与所有其他字段一起导入。
  • If checked, Stop on errors prevents any data from being imported in the event of an error. 如果选中“错误时停止”,此选项将防止在发生错误时导入任何数据。If unchecked, data is inserted until an error is encountered and successful inserts are not rolled back. 如果未选中,将插入数据,直到遇到错误且未回滚成功的插入。The import operation will not continue after encountering an error in either case.在任何情况下遇到错误后,导入操作都不会继续。
7

A progress bar displays the status of the import. 进度条显示导入的状态。If an error occurs during import, the progress bar turns red and an error message appears in the dialog. 如果在导入过程中发生错误,则进度条将变为红色,并在对话框中显示错误消息。After successful import, the dialog closes and Compass displays the collection page containing the newly imported documents.导入成功后,对话框关闭,Compass显示包含新导入文档的集合页面。

MongoDB Compass can export data from a collection as either a JSON or CSV file. MongoDB Compass可以将集合中的数据导出为JSON或CSV文件。If you specify a filter, Compass only exports documents which match the specified query.如果指定过滤器,Compass仅导出与指定查询匹配的文档。

While it is possible to exclude documents by using a query filter, it is not possible to re-shape exported documents with a project document. 虽然可以使用查询过滤器排除文档,但无法使用投影文档重新塑造导出文档的形状。Even when you specify a project option in the query, Compass still exports the entire document.即使在查询中指定项目选项,Compass仍会导出整个文档。

To import and export data from the command line, you can use MongoDB's Database Tools. 要从命令行导入和导出数据,可以使用MongoDB的数据库工具See mongoimport and mongoexport.请参见mongoimportmongoexport