Docs Home / Compass / Interact with Your Data / Analyze Data Schema

Export Your Schema

You can export your schema after analyzing it. This is useful for sharing your schema and comparing schemas across collections.

Before you Begin

If you have not already done so, analyze your schema:

1

Select the collection.

Select your desired collection and click the Schema tab.

2

Analyze the schema.

Click Analyze Schema.

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, use the following procedure to export your schema.

Steps

1

Click Export Schema.

undefinedImage showing Export Schema button

The Export Schema button appears in the top-left corner of the viewport.

2

Select the export format.

In the Export JSON Schema modal, select the format to export to.

Export JSON Schema modal

You can export your schema to the following formats:

3

Save your schema.

Select the location on your filesystem to save your schema to.

Standard Format Schema Object Properties

Standard format schema objects contain the following fields:

PropertyData typeDescription
typestring or arrayJSON type of this data type. For details, see the official JSON docs for type.
requiredarray of stringsFields that must appear in the schema. For details, see the official JSON docs for required.
propertiesobjectProperties for each field. Keys are property names and values are subschemas. For details, see the official JSON docs for properties.
itemsdocumentMetadata about elements in array fields. Metadata appears as embedded subschemas. For details, see the official JSON docs for items.

This is not an exhaustive list of all possible fields. For details on additional fields, see the official JSON Schema spec.

MongoDB Format Schema Object Properties

MongoDB format schema objects contain the following fields:

PropertyData typeDescription
bsonTypestring or array of stringsBSON type of this field.
requiredarray of stringsFields that must appear in the schema.
propertiesdocumentProperties for each field. Keys are property names and values are subschemas.
itemsdocumentMetadata about elements in array fields. Metadata appears as embedded subschemas.

This is not an exhaustive list of all possible fields. For details on additional fields, see the official JSON Schema spec.

Expanded Format Schema Object Properties

Expanded format schema objects contain these fields in addition to the Standard Schema fields:

PropertyData typeDescription
x-bsonTypestring or arrayBSON type of this field.
x-metadatadocumentDocument containing metadata about the field.
hasDuplicatesbooleantrue if a single value appears multiple times in this field. Otherwise false.
probabilityfloatProbability that the field exists in a random document.
countintegerNumber of documents from the sample that have this field.
x-sampleValuesarraySample values as Expanded JSON. Sample values are limited to the first 100 characters.

This is not an exhaustive list of all possible fields. For details on additional fields, see the official JSON Schema spec.

Limitations

Compass cannot export a schema that has more than 1000 distinct fields. If you try to export a schema with more than 1000 distinct fields, Compass returns an error.

Example Schema

The following example uses a collection of 3 documents, each with a title field and unique information about that movie:

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]

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 to your clipboard.

Copy the above JSON documents.

2

Select a collection.

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

Paste the documents.

In the JSON view of the dialog, paste the copied documents and click Insert.

The example above outputs the following schema:

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}

Sample values are limited to the first 100 characters.

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}