Docs HomeMongoDB Compass

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.要将数据导入集合或从集合导出数据,请从“数据库”选项卡中选择集合或单击左侧导航中的集合,导航到详细的集合视图。

Import Data into a Collection将数据导入集合

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

Limitations局限性

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

Format Your Data设置数据格式

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

When importing data from a JSON file, you can format your data as:从“JSON”文件导入数据时,您可以将数据格式化为:

  • Newline-delimited documents, or换行符分隔的文档,或
  • Comma-separated documents in an array数组中逗号分隔的文档
Example

The following newline-delimited .json file is formatted correctly:以下换行符分隔的json文件格式正确:

{ "type": "home", "number": "212-555-1234" }
{ "type": "cell", "number": "646-555-4567" }
{ "type": "office", "number": "202-555-0182"}

The following comma-separated .json array file is also formatted correctly:以下逗号分隔的.json数组文件的格式也正确:

[{ "type": "home", "number": "212-555-1234" }, { "type": "cell", "number": "646-555-4567" }, { "type": "office", "number": "202-555-0182"}]

Compass ignores line breaks in JSON arrays.Compass忽略JSON数组中的换行符。

Compass automatically generates ObjectIDs for these objects on import since no ObjectIDs were specified in the initial JSON.Compass在导入时自动为这些对象生成ObjectIDs,因为在初始JSON中没有指定ObjectID。

When importing data from a CSV file, the first line of the file must be a comma-separated list of your document field names. 从“CSV”文件导入数据时,文件的第一行必须是文档字段名称的逗号分隔列表。Subsequent lines in the file must be comma-separated field values in the order corresponding with the field order in the first line.文件中的后续行必须是逗号分隔的字段值,其顺序与第一行中的字段顺序相对应。

Example

The following .csv file imports three documents:以下.csv文件导入三个文档:

name,age,fav_color,pet
Jeff,25,green,Bongo
Alice,20,purple,Hazel
Tim,32,red,Lassie

MongoDB Compass automatically generates ObjectIDs for these objects on import since no ObjectIDs were specified in the initial CSV file.MongoDB Compass在导入时自动为这些对象生成ObjectIDs,因为在初始CSV文件中没有指定ObjectID。

Procedure过程

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

1

Connect to the deployment containing the collection you wish to import data into.连接到包含要将数据导入到的集合的部署。

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

2

Navigate to your target collection.导航到您的目标集合。

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

3

Click the Add Data dropdown and select Import JSON or CSV file.单击“添加数据”下拉列表,然后选择“导入JSON或CSV文件”。

4

Select the appropriate file type.选择适当的文件类型。

Select either a JSON or CSV file to import and click Select.选择要导入的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文件,可以在“指定字段和类型”下指定要导入的字段以及这些字段的类型。

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.要为字段选择类型,请使用该字段名称下方的下拉菜单。

5

Configure import options.配置导入选项。

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.如果未选中,则插入数据,直到遇到错误,并且不会回滚成功的插入。在任何一种情况下遇到错误后,导入操作都不会继续。
6

Click Import.单击“导入”。

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. 进度条显示导入的状态。如果在导入过程中发生错误,进度条将变为红色,并且对话框中会显示一条错误消息。To see all errors, click View Log.要查看所有错误,请单击“查看日志”。

After successful import, the dialog closes and Compass displays the collection page containing the newly imported documents.导入成功后,对话框将关闭,Compass将显示包含新导入文档的集合页面。

Export Data from a Collection从集合导出数据

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

Behavior行为

  • Avoid exporting to CSV files when possible. 尽可能避免导出为“CSV”文件。CSV files may lose type information and are not suitable for backing up your data.“CSV”文件可能会丢失类型信息,不适合备份您的数据。
  • You can use a query filter to export only the documents that match the filter.您可以使用查询筛选器仅导出与筛选器匹配的文档。
  • You can use the Project field in the query bar to specify the fields to return or export.可以使用查询栏中的“投影”字段来指定要返回或导出的字段。

Procedure过程

To export an entire collection to a file:要将整个集合导出到文件,请执行以下操作:

1

Connect to the deployment containing the collection you wish to export data from.连接到包含要从中导出数据的集合的部署。

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

2

Navigate to your desired collection.导航到您想要的集合。

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

3

Click the Export Data dropdown and select Export the full collection.单击“导出数据”下拉列表,然后选择“导出完整集合”。

4

Select your file type.选择您的文件类型。

You can select either JSON or CSV. 您可以选择JSONCSVIf you select JSON, you can expand the Advanced JSON Format dropdown and select from the following extended JSON formats:如果选择JSON,则可以展开“高级JSON格式”下拉列表,并从以下扩展JSON格式中进行选择:

JSON FormatJSON格式Description描述
Default Extended JSON默认的扩展JSONA string format that avoids any loss of BSON type information. 一种避免丢失BSON类型信息的字符串格式。This is the default Compass setting.这是默认的Compass设置。
Relaxed Extended JSON宽松的扩展JSON A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information.一种字符串格式,强调可读性和互操作性,而牺牲了类型保存。也就是说,从宽松格式到BSON的转换可能会丢失类型信息。
Warning

This format is not recommended for data integrity.为了数据完整性,不建议使用此格式。

Canonical Extended JSON规范的扩展JSONAA string format that emphasizes type preservation at the expense of readability and interoperability. 一种字符串格式,强调以可读性和互操作性为代价的类型保留。That is, conversion from canonical to BSON will generally preserve type information except in certain specific cases.也就是说,从规范到BSON的转换通常会保留类型信息,除非在某些特定情况下。
5

Click Export.单击“导出”。

Choose where to export the file and click Select.选择导出文件的位置,然后单击“选择”。

A progress bar displays the status of the export. 进度条显示导出的状态。If an error occurs during export, the progress bar turns red and an error message appears in the dialog. 如果在导出过程中发生错误,进度条将变为红色,并且对话框中会显示一条错误消息。After successful export, the dialog closes.成功导出后,对话框关闭。

To export a subset of documents from a collection:要从集合导出文档子集,请执行以下操作:

1

Connect to the deployment containing the collection you wish to export data from.连接到包含要从中导出数据的集合的部署。

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

2

Navigate to your desired collection.导航到您想要的集合。

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

3

Specify a filter in the query bar.在“查询栏”中指定一个筛选器。

Specify a filter in the query bar to export only the documents which match the filter.查询栏中指定一个筛选器,以仅导出与该筛选器匹配的文档。

4

Click the Export Data dropdown and select Export query results.单击“导出数据”下拉列表,然后选择“导出查询结果”。

The top section of the export dialog displays the query entered in the query bar.导出对话框的顶部显示在查询栏中输入的查询。

5

Select document fields to include in your exported file.选择要包含在导出文件中的文档字段。

You can choose to export All Fields or Select fields in table. 您可以选择导出表中的“所有字段”或“选择字段”。In the query bar, you can also use the Project field to specify the fields to return or export.在查询栏中,还可以使用“投影”字段指定要返回或导出的字段。

Only fields that are checked are included in the exported file.只有选中的字段才会包含在导出的文件中。

You can add document fields to include with the Add Field button if the field you want to include is not automatically detected.如果未自动检测到要包含的字段,则可以使用“添加字段”按钮添加要包含的文档字段。

Export data dialog, step 2
Note

Compass samples your collection to pre-populate a list of fields. Compass对您的集合进行采样,以预先填充字段列表。Fields which only appear in a small percentage of documents may not be automatically detected.仅出现在文档中一小部分的字段可能不会被自动检测到。

For details on sampling, see Sampling.有关采样的详细信息,请参阅采样

6

Select the appropriate file type.选择适当的文件类型。

Under Export File Type, select either JSON or CSV. 在“导出文件类型”下,选择JSONCSVIf you select JSON, you can expand the Advanced JSON Format dropdown and select from the following extended JSON formats:如果选择JSON,则可以展开“高级JSON格式”下拉列表,并从以下扩展JSON格式中进行选择:

JSON FormatJSON格式Description描述
Default Extended JSON默认的扩展JSONA string format that avoids any loss of BSON type information. 一种字符串格式,可避免丢失BSON类型信息。This is the default Compass setting.这是默认的Compass设置。
Relaxed Extended JSON宽松的扩展JSON A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information.一种字符串格式,强调可读性和互操作性,而牺牲了类型保存。也就是说,从宽松格式到BSON的转换可能会丢失类型信息。
Warning

This format is not recommended for data integrity.为了数据完整性,不建议使用此格式。

Canonical Extended JSON规范的扩展JSONA string format that emphasizes type preservation at the expense of readability and interoperability. 一种字符串格式,强调以可读性和互操作性为代价的类型保留。That is, conversion from canonical to BSON will generally preserve type information except in certain specific cases.也就是说,从规范到BSON的转换通常会保留类型信息,除非在某些特定情况下。
7

Click Export.单击“导出”。

Choose where to export the file and click Select.选择导出文件的位置,然后单击“选择”。

A progress bar displays the status of the export. If an error occurs during export, the progress bar turns red and an error message appears in the dialog. 进度条显示导出的状态。如果在导出过程中发生错误,进度条将变为红色,并且对话框中会显示一条错误消息。After successful export, the dialog closes.成功导出后,对话框关闭。

To export results from your aggregation pipeline:要从聚合管道导出结果,请执行以下操作:

1

Connect to the deployment containing the collection you wish to export data from.连接到包含要从中导出数据的集合的部署。

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

2

Navigate to your target collection.导航到您的目标集合。

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

3

Create an aggregation pipeline and run for results.创建聚合管道并运行以获取结果。

To learn how to create an aggregation pipeline, see Create an Aggregation Pipeline.要了解如何创建聚合管道,请参阅创建聚合管道

4

Click Export.单击“导出”。

5

Select the appropriate file type.选择适当的文件类型。

Under Export File Type, select either JSON or CSV. If you select JSON, you can expand the Advanced JSON Format dropdown and select from the following extended JSON formats:在“导出文件类型”下,选择JSONCSV。如果选择JSON,则可以展开“高级JSON格式”下拉列表,并从以下扩展JSON格式中进行选择:

JSON FormatJSON格式Description描述
Default Extended JSON默认的扩展JSONA string format that avoids any loss of BSON type information. 一种字符串格式,可避免丢失BSON类型信息。This is the default Compass setting.这是默认的Compass设置。
Relaxed Extended JSON宽松的扩展JSON A string format that emphasizes readability and interoperability at the expense of type preservation. 一种字符串格式,强调可读性和互操作性,而牺牲了类型保存。That is, conversion from relaxed format to BSON can lose type information.也就是说,从宽松格式到BSON的转换可能会丢失类型信息。
Warning

This format is not recommended for data integrity.为了数据完整性,不建议使用此格式。

Canonical Extended JSON规范的扩展JSONA string format that emphasizes type preservation at the expense of readability and interoperability. 一种字符串格式,强调以可读性和互操作性为代价的类型保留。That is, conversion from canonical to BSON will generally preserve type information except in certain specific cases.也就是说,从规范到BSON的转换通常会保留类型信息,除非在某些特定情况下。
6

Click Export.单击“导出”。

Import and Export Data from the Command Line从命令行导入和导出数据

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