Docs Menu
Docs Home
/
MongoDB Compass
/ /

Export Your Schema

On this page

  • Schema Object Properties
  • 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.

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

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

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

Back

Analyze Data Schema