重要
不允许在 MongoDB Compass Readonly Edition 中插入文档。
Compass 提供以下模式视图,用于将文档插入到您的集合中:
- shell 语法模式
- 写入或粘贴文档使用 MongoDB shell 语法,其允许未引用的字段名称和类型构造函数,例如
ObjectId()和ISODate()。使用此模式可将多个文档作为一个数组同时插入。 - Field-by-Field Editor
- 选择每个插入文档的单个字段值和类型。此模式仅支持一次插入一个文档。
- JSON Mode
- Write or paste documents in Extended JSON format, which requires quoted field names and represents BSON types as wrappers such as
{"$oid": "..."}. Use this mode to insert multiple documents at once as an array.
步骤
要将文档插入到集合中,请选择与所选视图模式相对应的标签页:
插入您的文档。
在 MongoDB shell 语法中,键入或粘贴要插入到集合中的文档。要插入多个文档,请输入以逗号分隔的文档数组。
例子
以下示例将单个文档插入到集合中。字段名称不加引号,_id、copies 和 lastCheckedOut 值使用 ObjectId()、NumberInt() 和 ISODate() 类型构造函数,而不是 Extended JSON 类型包装器:
{ _id: ObjectId('507f1f77bcf86cd799439011'), title: 'The Odyssey', author: 'Homer', copies: NumberInt('10'), lastCheckedOut: ISODate('2025-03-14T09:30:00Z') }
注意
If you do not specify an _id value in your document, Compass automatically generates an ObjectId for the _id field.
添加新字段。
要在文档中添加新字段,请将鼠标悬停在对话框中的行号上,然后单击 图标以在选定行后添加新字段。
当文本光标位于最后一个文档字段的值上时,您还可以按 Tab 键在文档末尾添加新字段。
注意
If you do not specify an _id value in your document, Compass automatically generates an ObjectId for the _id field.
插入您的文档。
在 JSON 格式中,键入或粘贴要插入到集合中的文档。要插入多个文档,请输入以逗号分隔的 JSON 文档数组。
例子
以下数组将 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 } ]
注意
If you do not specify an _id value in your document, Compass automatically generates an ObjectId for the _id field.