Docs HomeMongoDB Compass

Insert Documents插入文档

On this page本页内容

Important

Inserting documents is not permitted in MongoDB Compass Readonly Edition.“MongoDB Compass只读版”不允许插入文档。

Compass provides two ways to insert documents into your collections: JSON Mode and a Field-by-Field Editor.Compass提供了两种将文档插入集合的方法:JSON模式和字段编辑器。

JSON ModeJSON模式 (New in Compass 1.20Compass 1.20中的新增功能)
Allows you to write or paste JSON documents in the editor. 允许您在编辑器中写入或粘贴JSON文档。Use this mode to insert multiple documents at once as an array.使用此模式可以同时将多个文档作为数组插入。
Field-by-Field Editor逐字段编辑器
Provides a more interactive experience to create documents, allowing you to select individual field values and types. 为创建文档提供了更交互式的体验,允许您选择各个字段值和类型。This mode only supports inserting one document at a time.此模式一次只支持插入一个文档。

Procedure过程

To insert documents into your collection:要将文档插入集合,请执行以下操作:

  1. Click the Add Data dropdown and select Insert Document.单击“添加数据”下拉列表,然后选择“插入文档”。

    Insert button
  2. Select the appropriate view based on how you would like to insert documents.根据您希望插入文档的方式选择适当的视图。

    • Click the { } brackets for JSON view. This is the default view.单击{ }方括号以查看JSON视图。这是默认视图。
    • Click the list icon for Field-by-Field mode.单击“按字段”模式的列表图标。
    Insert document in JSON view
    1. In JSON format, type or paste the document(s) you want to insert into the collection. 在JSON格式中,键入或粘贴要插入到集合中的文档。To insert multiple documents, enter a comma-separated array of JSON documents.要插入多个文档,请输入一个以逗号分隔的JSON文档数组。
      Example

      The following array inserts 5 documents into the collection:以下数组将5个文档插入到集合中:

      [
      { "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 },
      { "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 },
      { "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 },
      { "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 },
      { "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 }
      ]
      Note

      If you do not provide an ObjectId in your document, Compass automatically generates an ObjectId.如果您没有在文档中提供ObjectId,Compass会自动生成ObjectId。

    2. Click Insert.单击“插入”。

    1. For each field in the document, select field type and fill in the field name and value.对于文档中的每个字段,选择字段类型并填写字段名称和值。
      Insert document results
      Note

      If you do not provide an ObjectId in your document, Compass automatically generates an ObjectId.如果您没有在文档中提供ObjectId,Compass会自动生成ObjectId。

    Add New Fields添加新字段

    To add a new field in the document, hover over the row number in the dialog (the row number is not part of the document but the dialog display) and click the icon to add a new field after the selected row.要在文档中添加新字段,请将鼠标悬停在对话框中的行号上(行号不是文档的一部分,而是对话框显示的一部分),然后单击+图标在选定行之后添加新字段。

    You can also add a new field at the end of the document by pressing the tab key when your text cursor is in the value of the last document field.当文本游标位于最后一个文档字段的值时,也可以通过按tab键在文档末尾添加新字段。

    Insert new fields

    Change Field Type更改字段类型

    You can change the data type of a field by using the data type selectors on the right of the field.您可以使用字段右侧的数据类型选择器来更改字段的数据类型。

    Change type

    To change the _id field to use a custom value, change the data type from ObjectID to string and then overwrite the _id value:要将_id字段更改为使用自定义值,请将数据类型从ObjectID更改为string,然后覆盖_id值:

    Change type results
    1. Click Insert.单击“插入”。

Limitation限制

The Insert Document button is not available if you are connected to a Data Lake.如果连接到数据湖,则“插入文档”按钮不可用。