Docs Menu
Docs Home
/
MongoDB Compass
/ /

Export Your Schema

On this page

  • Before you Begin
  • Steps
  • Standard Format Schema Object Properties
  • MongoDB Format Schema Object Properties
  • Expanded Format Schema Object Properties
  • Limitations
  • Example 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.

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.

1
Image showing Export Schema button

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

2

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

Export JSON Schema modal
click to enlarge

You can export your schema to the following formats:

  • Standard format

  • MongoDB format

  • Expanded format

3

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

Standard format schema objects contain the following fields:

Property
Data type
Description

type

string or array

required

array of strings

Fields that must appear in the schema. For details, see the official JSON docs.

properties

object

Properties for each field. Keys are property names and values are subschemas. For details, see the official JSON docs.

items

document

Metadata about elements in array fields. Metadata appears as embedded subschemas. For details, see the official JSON docs.

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

MongoDB format schema objects contain the following fields:

Property
Data type
Description

bsonType

string or array of strings

BSON type of this field.

required

array of strings

Fields that must appear in the schema.

properties

document

Properties for each field. Keys are property names and values are subschemas.

items

document

Metadata 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 objects contain these fields in addition to the Standard Schema fields:

Property
Data type
Description

x-bsonType

string or array

BSON type of this field.

x-metadata

document

Document containing metadata about the field.

hasDuplicates

boolean

true if a single value appears multiple times in this field. Otherwise false.

probability

float

Probability that the field exists in a random document.

count

integer

Number of documents from the sample that have this field.

x-sampleValues

array

Sample 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.

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.

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 above JSON documents.

2

In MongoDB Compass, select a collection or create a new collection to import the copied documents to. The Documents tab displays.

3
4
5

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}

Back

Analyze Data Schema