Navigation

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. 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.
Image showing Collection dropdown

Your schema is copied to your clipboard as a JSON object.

Schema Object Properties

Schema objects have count and fields properties:

  • 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:
Property Data type Description
name String Name of the corresponding field, e.g. _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.
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.
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.
probability Number Probability 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[
  {
    "_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. To import the example collection into MongoDB Compass:

  1. Copy the JSON documents above.
  2. In MongoDB Compass, select a collection or create a new collection to import the copied documents to. 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.

The example above outputs the following schema:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
{
  "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
}