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 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.count是一个整数,表示从集合中采样以生成架构的文档数。
  • fields is an array of metadata objects that correspond to each field in the documents analyzed during sampling. fields是元数据对象的数组,对应于采样期间分析的文档中的每个字段。Each element in the fields array contains the following fields:fields数组中的每个元素都包含以下字段:
Property属性
Data type数据类型
Description描述
name
String
Name of the corresponding field, e.g. _id.对应字段的名称,例如_id
path
String
Path to the corresponding field within a document.文档中相应字段的路径。
count
Integer
Number of documents in which the corresponding field appears.显示相应字段的文档数。
types
Array
Array of metadata objects that represent each data type that appears in the corresponding field.表示相应字段中显示的每个数据类型的元数据对象数组。
types[n].name
String
Name of this data type.此数据类型的名称。
types[n].bsonType
String
BSON type of this data type.此数据类型的BSON类型。
types[n].path
String
Path to the corresponding field within a document.文档中相应字段的路径。
types[n].count
Integer
Number of times this data type appears in the corresponding field.此数据类型出现在相应字段中的次数。
types[n].values
Array
Array of the actual sampled values that appear in the corresponding field and match this data type.出现在相应字段中并与此数据类型匹配的实际采样值的数组。
types[n].total_count
Integer
If the corresponding field is an array, the number of elements in that array.如果对应字段是数组,则为该数组中的元素数。
types[n].probability
Number
Probability that the value of the corresponding field is this data type in a random document.对应字段的值是随机文档中此数据类型的概率。
types[n].unique
Integer
Number of unique values of this data type that appear in the corresponding field.出现在相应字段中的此数据类型的唯一值的数目。
types[n].has_duplicates
Boolean
true if a single value of this data type appears multiple times in the corresponding field. Otherwise false.如果此数据类型的单个值在相应字段中多次出现,则为true。否则为false
types[n].lengths
Array
If 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_length
Number
If 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_count
Integer
Number of documents sampled from the collection.从集合中采样的文档数。
type
String or Array
String or array of strings representing possible types for the corresponding field.表示相应字段的可能类型的字符串或字符串数组。
has_duplicates
Boolean
true if a single value appears multiple times in the corresponding field. Otherwise false.如果单个值在相应字段中多次出现,则为true。否则为false
probability
Number
Probability that a random document contains the corresponding field.随机文档包含相应字段的概率。

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
}