Docs 菜单

导出模式

可以在分析后导出模式。这对于共享模式和跨集合比较模式很有用。

如果还没有这样做,请先分析模式:

1

选择您所需的集合并单击 Schema 标签页。

2

单击 Analyze Schema(连接)。

当 Compass 分析您的模式时,它会从您的集合中随机抽取文档子集进行采样。要了解有关采样的更多信息,请参阅采样

分析模式后,使用以下过程导出模式。

1
Image showing Export Schema button

Export Schema 按钮出现在视口的左上角。

2

Export JSON Schema 模式中,选择要导出的格式。

Export JSON Schema modal
点击放大

您可以将模式导出为以下格式:

3

在文件系统上选择要保存模式的位置。

标准格式模式对象包含以下字段:

属性
数据类型
说明

type

字符串或大量

此数据类型的JSON类型。有关详细信息,请参阅类型的官方JSON文档

required

字符串数组

必须出现在模式中的字段。有关详细信息,请参阅所需的官方JSON文档

properties

对象

每个字段的属性。键是属性名称,值是子模式。有关详细信息,请参阅 properties 的官方JSON文档

items

文档

有关大量字段中元素的元数据。元数据显示为嵌入式子模式。有关详细信息,请参阅 items 的官方JSON文档

这并不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方JSON schema 规范

MongoDB格式模式对象包含以下字段:

属性
数据类型
说明

bsonType

字符串或字符串数组

该字段的BSON类型。

required

字符串数组

必须出现在模式中的字段。

properties

文档

每个字段的属性。键是属性名称,值是子模式。

items

文档

有关大量字段中元素的元数据。元数据显示为嵌入式子模式。

这并不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方JSON schema 规范

除了标准模式字段之外,扩展格式模式对象还包含以下字段:

属性
数据类型
说明

x-bsonType

字符串或大量

该字段的BSON类型。

x-metadata

文档

包含字段元数据的文档。

hasDuplicates

布尔

如果同一个值在此字段中出现多次,则为 true。否则为 false

probability

float

该字段存在于随机文档中的概率。

count

整型

示例中具有此字段的文档数量。

x-sampleValues

阵列

以扩展JSON形式对值进行采样。示例值仅限于前 100 个字符。

这并不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方JSON schema 规范

Compass无法导出具有超过 1000 个不同字段的模式。如果您尝试导出包含超过 1000 个不同字段的模式, Compass将返回错误。

以下示例使用 3 文档集合,每个文档都有一个 title字段和有关该电影的唯一信息:

1[
2 {
3 "_id": { "$oid": "573a1390f29313caabcd6223" },
4 "title": "The Poor Little Rich Girl",
5 "plot": "Gwen's family is rich, but her parents ignore her and most of the serv...",
6 "year": 1917,
7 },
8 {
9 "_id": { "$oid": "573a1391f29313caabcd7616" },
10 "title": "Salomè",
11 "plot": "Salome, the daughter of Herodias, seduces her step-father/uncle Herod, ...",
12 "year": 1922,
13 "genres": [ "drama", "horror" ]
14 },
15 {
16 "_id": { "$oid": "573a1392f29313caabcd9c1b" },
17 "title": "Payment Deferred",
18 "year": 1932,
19 },
20]

您可以将以上示例导入 MongoDB Compass 来试验模式输出。要将示例集合导入 MongoDB Compass:

1

复制上述JSON文档。

2

在 MongoDB Compass 中,选择一个集合或创建一个新集合以将复制的文档导入其中。随即显示 Documents(文档)标签页。

3
4
5

在对话框的 JSON 视图中粘贴复制的文档,然后单击 Insert

上面的示例输出以下模式:

1{
2 "$schema": "https://json-schema.org/draft/2020-12/schema",
3 "type": "object",
4 "required": [
5 "_id",
6 "title",
7 "year"
8 ],
9 "properties": {
10 "_id": {
11 "$ref": "#/$defs/ObjectId"
12 },
13 "genres": {
14 "type": "array",
15 "items": {
16 "type": "string"
17 }
18 },
19 "plot": {
20 "type": "string"
21 },
22 "title": {
23 "type": "string"
24 },
25 "year": {
26 "type": "integer"
27 }
28 },
29 "$defs": {
30 "ObjectId": {
31 "type": "object",
32 "properties": {
33 "$oid": {
34 "type": "string",
35 "pattern": "^[0-9a-fA-F]{24}$"
36 }
37 },
38 "required": [
39 "$oid"
40 ],
41 "additionalProperties": false
42 }
43 }
44}
1{
2 "$jsonSchema": {
3 "bsonType": "object",
4 "required": [
5 "_id",
6 "title",
7 "year"
8 ],
9 "properties": {
10 "_id": {
11 "bsonType": "objectId"
12 },
13 "genres": {
14 "bsonType": "array",
15 "items": {
16 "bsonType": "string"
17 }
18 },
19 "plot": {
20 "bsonType": "string"
21 },
22 "title": {
23 "bsonType": "string"
24 },
25 "year": {
26 "bsonType": "int"
27 }
28 }
29 }
30}

示例值仅限于前 100 个字符。

1{
2 "type": "object",
3 "x-bsonType": "object",
4 "required": [
5 "_id",
6 "title",
7 "year"
8 ],
9 "properties": {
10 "_id": {
11 "$ref": "#/$defs/ObjectId",
12 "x-bsonType": "objectId",
13 "x-metadata": {
14 "hasDuplicates": false,
15 "probability": 1,
16 "count": 3
17 },
18 "x-sampleValues": [
19 "573a1391f29313caabcd7616",
20 "573a1392f29313caabcd9c1b",
21 "573a1390f29313caabcd6223"
22 ]
23 },
24 "genres": {
25 "type": "array",
26 "x-bsonType": "array",
27 "x-metadata": {
28 "hasDuplicates": true,
29 "probability": 0.3333333333333333,
30 "count": 1
31 },
32 "items": {
33 "type": "string",
34 "x-bsonType": "string",
35 "x-metadata": {
36 "hasDuplicates": false,
37 "probability": 1,
38 "count": 2
39 },
40 "x-sampleValues": [
41 "drama",
42 "horror"
43 ]
44 }
45 },
46 "plot": {
47 "type": "string",
48 "x-bsonType": "string",
49 "x-metadata": {
50 "hasDuplicates": false,
51 "probability": 0.6666666666666666,
52 "count": 2
53 },
54 "x-sampleValues": [
55 "Salome, the daughter of Herodias, seduces her step-father/uncle Herod, ...",
56 "Gwen's family is rich, but her parents ignore her and most of the serv..."
57 ]
58 },
59 "title": {
60 "type": "string",
61 "x-bsonType": "string",
62 "x-metadata": {
63 "hasDuplicates": false,
64 "probability": 1,
65 "count": 3
66 },
67 "x-sampleValues": [
68 "Salomè",
69 "Payment Deferred",
70 "The Poor Little Rich Girl"
71 ]
72 },
73 "year": {
74 "type": "integer",
75 "x-bsonType": "int",
76 "x-metadata": {
77 "hasDuplicates": false,
78 "probability": 1,
79 "count": 3
80 },
81 "x-sampleValues": [
82 1922,
83 1932,
84 1917
85 ]
86 }
87 },
88 "$defs": {
89 "ObjectId": {
90 "type": "object",
91 "properties": {
92 "$oid": {
93 "type": "string",
94 "pattern": "^[0-9a-fA-F]{24}$"
95 }
96 },
97 "required": [
98 "$oid"
99 ],
100 "additionalProperties": false
101 }
102 }
103}