Docs Home / Compass / Interact with Your Data / Analyze Data Schema

Export Your Schema导出模式

You can export your schema after analyzing it. This is useful for sharing your schema and comparing schemas across collections.您可以在分析模式后导出它。这对于共享模式和跨集合比较模式非常有用。

Before you Begin开始之前

If you have not already done so, analyze your schema:如果您还没有这样做,请分析模式:

1

Select the collection.选择集合。

Select your desired collection and click the Schema tab.选择所需的集合,然后单击“模式”选项卡。

2

Analyze the schema.分析模式。

Click Analyze Schema.单击分析模式。

When Compass analyzes your schema, it samples a random subset of documents from your collection. To learn more about sampling, see Sampling.Compass分析模式时,会从集合中随机抽取一部分文档。要了解有关采样的更多信息,请参阅采样

Once your schema has been analyzed, use the following procedure to export your schema.分析完模式后,使用以下过程导出模式。

Steps步骤

1

Click Export Schema.单击“导出模式”。

Image showing Export Schema button

The Export Schema button appears in the top-left corner of the viewport.“导出模式”按钮出现在视口的左上角。

2

Select the export format.选择导出格式。

In the Export JSON Schema modal, select the format to export to.在导出JSON模式中,选择要导出的格式。

Export JSON Schema modal

You can export your schema to the following formats:您可以将模式导出为以下格式:

3

Save your schema.保存模式。

Select the location on your filesystem to save your schema to.在文件系统上选择要保存模式的位置。

Standard Format Schema Object Properties标准格式模式对象属性

Standard format schema objects contain the following fields:标准格式模式对象包含以下字段:

Property属性Data type数据类型Description描述
typestring or arrayJSON type of this data type. For details, see the official JSON docs for type.此数据类型的JSON类型。有关详细信息,请参阅类型的官方JSON文档
requiredarray of stringsFields that must appear in the schema. For details, see the official JSON docs for required.必须出现在模式中的字段。有关详细信息,请参阅所需的官方JSON文档
propertiesobjectProperties for each field. Keys are property names and values are subschemas. For details, see the official JSON docs for properties.每个字段的属性。键是属性名称,值是子值。有关详细信息,请参阅属性的官方JSON文档
itemsdocumentMetadata about elements in array fields. Metadata appears as embedded subschemas. For details, see the official JSON docs for items.关于数组字段中元素的元数据。元数据显示为嵌入式子模式。有关详细信息,请参阅项目的官方JSON文档。

This is not an exhaustive list of all possible fields. For details on additional fields, see the official JSON Schema spec.这不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方的JSON Schema规范

MongoDB Format Schema Object PropertiesMongoDB格式模式对象属性

MongoDB format schema objects contain the following fields:MongoDB格式模式对象包含以下字段:

Property属性Data type数据类型Description描述
bsonTypestring or array of stringsBSON type of this field.此字段的BSON类型。
requiredarray of stringsFields that must appear in the schema.必须出现在架构中的字段。
propertiesdocumentProperties for each field. Keys are property names and values are subschemas.每个字段的属性。键是属性名称,值是子值。
itemsdocumentMetadata about elements in array fields. Metadata appears as embedded subschemas.关于数组字段中元素的元数据。元数据显示为嵌入式子模式。关于数组字段中元素的元数据。元数据显示为嵌入式子模式。

This is not an exhaustive list of all possible fields. For details on additional fields, see the official JSON Schema spec.这不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方的JSON Schema规范

Expanded Format Schema Object Properties扩展格式架构对象属性

Expanded format schema objects contain these fields in addition to the Standard Schema fields:除了标准模式字段外,扩展格式模式对象还包含以下字段:

Property属性Data type数据类型Description描述
x-bsonTypestring or arrayBSON type of this field.此字段的BSON类型。
x-metadatadocumentDocument containing metadata about the field.包含字段元数据的文档。
hasDuplicatesbooleantrue if a single value appears multiple times in this field. Otherwise false.如果一个值在此字段中多次出现,则为true。否则就是假的。
probabilityfloatProbability that the field exists in a random document.该字段存在于随机文档中的概率。
countintegerNumber of documents from the sample that have this field.样本中具有此字段的文档数。
x-sampleValuesarraySample values as Expanded JSON. Sample values are limited to the first 100 characters.示例值为扩展JSON。样本值限制为前100个字符。

This is not an exhaustive list of all possible fields. For details on additional fields, see the official JSON Schema spec.这不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方的JSON Schema规范

Limitations局限性

Compass cannot export a schema that has more than 1000 distinct fields. If you try to export a schema with more than 1000 distinct fields, Compass returns an error.Compass无法导出具有1000多个不同字段的模式。如果尝试导出具有1000多个不同字段的模式,Compass将返回错误。

Example Schema示例模式

The following example uses a collection of 3 documents, each with a title field and unique information about that movie:以下示例使用3个文档的集合,每个文档都有一个title字段和关于该电影的唯一信息:

[
{
"_id": { "$oid": "573a1390f29313caabcd6223" },
"title": "The Poor Little Rich Girl",
"plot": "Gwen's family is rich, but her parents ignore her and most of the serv...",
"year": 1917,
},
{
"_id": { "$oid": "573a1391f29313caabcd7616" },
"title": "Salomè",
"plot": "Salome, the daughter of Herodias, seduces her step-father/uncle Herod, ...",
"year": 1922,
"genres": [ "drama", "horror" ]
},
{
"_id": { "$oid": "573a1392f29313caabcd9c1b" },
"title": "Payment Deferred",
"year": 1932,
},
]

You can import the above example to MongoDB Compass to experiment with schema outputs. To import the example collection into MongoDB Compass:您可以将上述示例导入MongoDB Compass,以尝试模式输出。要将示例集合导入MongoDB Compass:

1

Copy the JSON documents to your clipboard.将JSON文档复制到剪贴板。

Copy the above JSON documents.复制上述JSON文档

2

Select a collection.选择一个集合。

In MongoDB Compass, select a collection or create a new collection to import the copied documents to. The Documents tab displays.在MongoDB Compass中,选择一个集合或创建一个新集合以导入复制的文档。将显示“文档”选项卡。

3

Click Add Data.单击“添加数据”。

4

Select Insert Document from the dropdown.从下拉列表中选择“插入文档”。

5

Paste the documents.粘贴文档。

In the JSON view of the dialog, paste the copied documents and click Insert.在对话框的JSON视图中,粘贴复制的文档并单击“插入”。

The example above outputs the following schema:上面的示例输出以下模式:

Standard Export Format标准导出格式
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": [
"_id",
"title",
"year"
],
"properties": {
"_id": {
"$ref": "#/$defs/ObjectId"
},
"genres": {
"type": "array",
"items": {
"type": "string"
}
},
"plot": {
"type": "string"
},
"title": {
"type": "string"
},
"year": {
"type": "integer"
}
},
"$defs": {
"ObjectId": {
"type": "object",
"properties": {
"$oid": {
"type": "string",
"pattern": "^[0-9a-fA-F]{24}$"
}
},
"required": [
"$oid"
],
"additionalProperties": false
}
}
}
MongoDB Export FormatMongoDB导出格式
{
"$jsonSchema": {
"bsonType": "object",
"required": [
"_id",
"title",
"year"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"genres": {
"bsonType": "array",
"items": {
"bsonType": "string"
}
},
"plot": {
"bsonType": "string"
},
"title": {
"bsonType": "string"
},
"year": {
"bsonType": "int"
}
}
}
}
Expanded Export Format扩展导出格式

Sample values are limited to the first 100 characters.样本值限制为前100个字符。

{
"type": "object",
"x-bsonType": "object",
"required": [
"_id",
"title",
"year"
],
"properties": {
"_id": {
"$ref": "#/$defs/ObjectId",
"x-bsonType": "objectId",
"x-metadata": {
"hasDuplicates": false,
"probability": 1,
"count": 3
},
"x-sampleValues": [
"573a1391f29313caabcd7616",
"573a1392f29313caabcd9c1b",
"573a1390f29313caabcd6223"
]
},
"genres": {
"type": "array",
"x-bsonType": "array",
"x-metadata": {
"hasDuplicates": true,
"probability": 0.3333333333333333,
"count": 1
},
"items": {
"type": "string",
"x-bsonType": "string",
"x-metadata": {
"hasDuplicates": false,
"probability": 1,
"count": 2
},
"x-sampleValues": [
"drama",
"horror"
]
}
},
"plot": {
"type": "string",
"x-bsonType": "string",
"x-metadata": {
"hasDuplicates": false,
"probability": 0.6666666666666666,
"count": 2
},
"x-sampleValues": [
"Salome, the daughter of Herodias, seduces her step-father/uncle Herod, ...",
"Gwen's family is rich, but her parents ignore her and most of the serv..."
]
},
"title": {
"type": "string",
"x-bsonType": "string",
"x-metadata": {
"hasDuplicates": false,
"probability": 1,
"count": 3
},
"x-sampleValues": [
"Salomè",
"Payment Deferred",
"The Poor Little Rich Girl"
]
},
"year": {
"type": "integer",
"x-bsonType": "int",
"x-metadata": {
"hasDuplicates": false,
"probability": 1,
"count": 3
},
"x-sampleValues": [
1922,
1932,
1917
]
}
},
"$defs": {
"ObjectId": {
"type": "object",
"properties": {
"$oid": {
"type": "string",
"pattern": "^[0-9a-fA-F]{24}$"
}
},
"required": [
"$oid"
],
"additionalProperties": false
}
}
}