可以在分析后导出模式。这对于共享模式和跨集合比较模式很有用。
开始之前
如果还没有这样做,请先分析模式:
完成架构分析后,请按以下步骤导出架构。
步骤
标准格式模式对象属性
标准格式模式对象包含以下字段:
属性 | 数据类型 | 说明 |
|---|---|---|
| 字符串或数组 | 此数据类型的JSON类型。有关详细信息,请参阅 类型 的官方JSONDocs。 |
| 字符串数组 | 必须出现在模式中的字段。有关详细信息,请参阅官方 JSON 文档。 |
| 对象 | 每个字段的属性。键是属性名称,值是子模式。有关详细信息,请参阅 properties 的官方JSONDocs。 |
| 文档 | 关于数组字段中元素的元数据。元数据显示为嵌入式子模式。有关详情,请参阅官方 JSON 文档。 |
这并不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方JSON schema 规范。
MongoDB 格式模式对象属性
MongoDB格式模式对象包含以下字段:
属性 | 数据类型 | 说明 |
|---|---|---|
| 字符串或字符串数组 | 该字段的 BSON 类型。 |
| 字符串数组 | 必须出现在架构中的字段。 |
| 文档 | 每个字段的属性。键是属性名称,值是子模式。 |
| 文档 | 有关大量字段中元素的元数据。元数据显示为嵌入式子模式。 |
这并不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方JSON schema 规范。
扩展格式模式对象属性
扩展格式模式对象除了标准模式字段外,还包含以下字段:
属性 | 数据类型 | 说明 |
|---|---|---|
| 字符串或数组 | 该字段的 BSON 类型。 |
| 文档 | 包含字段元数据的文档。 |
| 布尔 |
|
| float | 字段在随机文档中存在的概率。 |
| 整型 | 示例中具有此字段的文档数量。 |
| 阵列 | 以扩展JSON形式对值进行采样。示例值仅限于前 100 个字符。 |
这并不是所有可能字段的详尽列表。有关其他字段的详细信息,请参阅官方JSON schema 规范。
限制
Compass 无法导出包含超过 1000 个不同字段的模式。如果您尝试导出包含超过 1000 个不同字段的模式,Compass 会返回错误。
示例模式
以下示例使用 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, },]您可以将以上示例导入 MongoDB Compass 来试验模式输出。要将示例集合导入 MongoDB Compass:
将JSON文档复制到剪贴板。
复制上面的 JSON 文档。
上面的示例输出以下模式:
{ "$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 } }}{ "$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" } } }}示例值仅限于前 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 } }}
