Insert Documents插入文档

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 Mode (New in Compass 1.20)JSON模式(Compass 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.此模式一次仅支持插入一个文档。

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.单击JSON视图的{}括号。This is the default view.这是默认视图。
    • 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。

    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

    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.单击“插入”。

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