Docs HomeMongoDB Compass

Export Your Schema导出架构

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

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

  1. Select your desired collection and click the Schema tab.选择所需的集合,然后单击“架构”选项卡。
  2. Click Analyze Schema.单击“分析架构”。
Note

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

Once your schema has been analyzed, export your schema:分析完架构后,导出架构:

  1. In the top menu bar, click Collection.在顶部菜单栏中,单击“集合”。
  2. From the dropdown, click Share Schema as JSON.从下拉列表中,单击“将架构共享为JSON”。
Image showing Collection dropdown

Your schema is copied to your clipboard as a JSON object.架构将作为JSON对象复制到剪贴板。

Schema Object Properties架构对象属性

Schema objects have count and fields properties:架构对象具有countfields属性:

  • count is an integer that represents the number of documents sampled from the collection to generate the schema.是一个整数,表示从集合中采样以生成架构的文档数。
  • fields is an array of metadata objects that correspond to each field in the documents analyzed during sampling. 是与采样期间分析的文档中的每个字段相对应的元数据对象数组。Each element in the fields array contains the following fields:fields数组中的每个元素都包含以下字段:
Property属性Data type数据类型Description描述
nameStringName of the corresponding field, e.g. _id.相应字段的名称,例如_id
pathStringPath to the corresponding field within a document.文档中相应字段的路径。
countIntegerNumber of documents in which the corresponding field appears.出现相应字段的文档数。
typesArrayArray of metadata objects that represent each data type that appears in the corresponding field.元数据对象的数组,表示出现在相应字段中的每种数据类型。
types[n].nameStringName of this data type.此数据类型的名称。
types[n].bsonTypeStringBSON type of this data type.此数据类型的BSON类型。
types[n].pathStringPath to the corresponding field within a document.文档中相应字段的路径。
types[n].countIntegerNumber of times this data type appears in the corresponding field.此数据类型在相应字段中显示的次数。
types[n].valuesArrayArray of the actual sampled values that appear in the corresponding field and match this data type.出现在相应字段中并与此数据类型匹配的实际采样值的数组。
types[n].total_countIntegerIf the corresponding field is an array, the number of elements in that array.如果对应的字段是一个数组,则表示该数组中的元素数。
types[n].probabilityNumberProbability that the value of the corresponding field is this data type in a random document.在随机文档中,相应字段的值为该数据类型的概率。
types[n].uniqueIntegerNumber of unique values of this data type that appear in the corresponding field.出现在相应字段中的此数据类型的唯一值的数目。
types[n].has_duplicatesBooleantrue if a single value of this data type appears multiple times in the corresponding field. 如果此数据类型的单个值在相应字段中多次出现,则为trueOtherwise false.否则为false
types[n].lengthsArrayIf this data type is an array, an array of integers representing the lengths of arrays found in the corresponding field. 如果此数据类型是数组,则表示在相应字段中找到的数组长度的整数数组。Not present for other data types.不适用于其他数据类型。
types[n].average_lengthNumberIf this data type is an array, the average length of arrays in the corresponding field across sampled documents. 如果此数据类型是数组,则为采样文档中相应字段中数组的平均长度。Not present for other data types.不适用于其他数据类型。
total_countIntegerNumber of documents sampled from the collection.从集合中采样的文档数。
typeString or ArrayString or array of strings representing possible types for the corresponding field.字符串或字符串数组,表示相应字段的可能类型。
has_duplicatesBooleantrue if a single value appears multiple times in the corresponding field. 如果单个值在相应字段中多次出现,则为trueOtherwise false.否则为false
probabilityNumberProbability that a random document contains the corresponding field.随机文档包含相应字段的概率。

Example Schema架构示例

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

[
{
"_id": { "$oid":"5e8359ba7782b98ba98c16fd" },
"sport": "Baseball",
"equipment": [ "bat", "baseball", "glove", "helmet" ]
},
{
"_id": { "$oid":"5e835a727782b98ba98c16fe" },
"sport": "Football",
"variants": {
"us":"Soccer",
"eu":"Football"
}
},
{
"_id": { "$oid":"5e835ade7782b98ba98c16ff" },
"sport": "Cricket",
"origin": "England"
}
]

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

  1. Copy the JSON documents above.复制上面的JSON文档。
  2. In MongoDB Compass, select a collection or create a new collection to import the copied documents to. 在MongoDB Compass中,选择一个集合或创建一个新集合来导入复制的文档。The Documents tab displays.此时将显示“文档”选项卡。
  3. Click Add Data.单击“添加数据”。
  4. Select Insert Document from the dropdown.从下拉列表中选择“插入文档”。
  5. In the JSON view of the dialog, paste the copied documents and click Insert.在对话框的JSON视图中,粘贴复制的文档,然后单击“插入”。

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


{
"fields": [
{
"name": "_id",
"path": "_id",
"count": 3,
"types": [
{
"name": "ObjectID",
"bsonType": "ObjectID",
"path": "_id",
"count": 3,
"values": [
"5e8359ba7782b98ba98c16fd",
"5e835a727782b98ba98c16fe",
"5e835ade7782b98ba98c16ff"
],
"total_count": 0,
"probability": 1,
"unique": 3,
"has_duplicates": false
}
],
"total_count": 3,
"type": "ObjectID",
"has_duplicates": false,
"probability": 1
},
{
"name": "equipment",
"path": "equipment",
"count": 1,
"types": [
{
"name": "Undefined",
"type": "Undefined",
"path": "equipment",
"count": 2,
"total_count": 0,
"probability": 0.6666666666666666,
"unique": 1,
"has_duplicates": true
},
{
"name": "Array",
"bsonType": "Array",
"path": "equipment",
"count": 1,
"types": [
{
"name": "String",
"bsonType": "String",
"path": "equipment",
"count": 4,
"values": [
"bat",
"baseball",
"glove",
"helmet"
],
"total_count": 0,
"probability": 1,
"unique": 4,
"has_duplicates": false
}
],
"lengths": [
4
],
"total_count": 4,
"probability": 0.3333333333333333,
"average_length": 4
}
],
"total_count": 3,
"type": [
"Undefined",
"Array"
],
"has_duplicates": true,
"probability": 0.3333333333333333
},
{
"name": "origin",
"path": "origin",
"count": 1,
"types": [
{
"name": "Undefined",
"type": "Undefined",
"path": "origin",
"count": 2,
"total_count": 0,
"probability": 0.6666666666666666,
"unique": 1,
"has_duplicates": true
},
{
"name": "String",
"bsonType": "String",
"path": "origin",
"count": 1,
"values": [
"England"
],
"total_count": 0,
"probability": 0.3333333333333333,
"unique": 1,
"has_duplicates": false
}
],
"total_count": 3,
"type": [
"Undefined",
"String"
],
"has_duplicates": true,
"probability": 0.3333333333333333
},
{
"name": "sport",
"path": "sport",
"count": 3,
"types": [
{
"name": "String",
"bsonType": "String",
"path": "sport",
"count": 3,
"values": [
"Baseball",
"Football",
"Cricket"
],
"total_count": 0,
"probability": 1,
"unique": 3,
"has_duplicates": false
}
],
"total_count": 3,
"type": "String",
"has_duplicates": false,
"probability": 1
},
{
"name": "variants",
"path": "variants",
"count": 1,
"types": [
{
"name": "Undefined",
"type": "Undefined",
"path": "variants",
"count": 2,
"total_count": 0,
"probability": 0.6666666666666666,
"unique": 1,
"has_duplicates": true
},
{
"name": "Document",
"bsonType": "Document",
"path": "variants",
"count": 1,
"fields": [
{
"name": "eu",
"path": "variants.eu",
"count": 1,
"types": [
{
"name": "String",
"bsonType": "String",
"path": "variants.eu",
"count": 1,
"values": [
"Football"
],
"total_count": 0,
"probability": 1,
"unique": 1,
"has_duplicates": false
}
],
"total_count": 1,
"type": "String",
"has_duplicates": false,
"probability": 1
},
{
"name": "us",
"path": "variants.us",
"count": 1,
"types": [
{
"name": "String",
"bsonType": "String",
"path": "variants.us",
"count": 1,
"values": [
"Soccer"
],
"total_count": 0,
"probability": 1,
"unique": 1,
"has_duplicates": false
}
],
"total_count": 1,
"type": "String",
"has_duplicates": false,
"probability": 1
}
],
"total_count": 0,
"probability": 0.3333333333333333
}
],
"total_count": 3,
"type": [
"Undefined",
"Document"
],
"has_duplicates": true,
"probability": 0.3333333333333333
}
],
"count": 3
}