> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  other tabs: standard-export-format, mongodb-export-format, expanded-export-format
-->

# Export Your Schema

MongoDB Compass Readonly Edition will be deprecated in a future release. To replicate the read-only behavior in a standard Compass edition, assign users [`read`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-read) access at the database level and enable the [readOnly](https://www.mongodb.com/docs/compass/settings/read-only.md#std-label-compass-read-only) option in Compass.

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.](https://www.mongodb.com/docs/compass/sampling.md#std-label-compass-sampling)

Once your schema has been analyzed, use the following procedure to export your schema.

## Steps

1. Click Export Schema.

   ![Image showing Export Schema button](/images/compass/export-schema-button.png)

   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](/images/compass/export-modal.png)

   You can export your schema to the following formats:

   - [Standard](https://www.mongodb.com/docs/compass/schema/export.md#std-label-export-format-standard) format

   - [MongoDB](https://www.mongodb.com/docs/compass/schema/export.md#std-label-export-format-mongodb) format

   - [Expanded](https://www.mongodb.com/docs/compass/schema/export.md#std-label-export-format-expanded) format

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:

| Property | Data type | Description |
| --- | --- | --- |
| `type` | string or array | JSON type of this data type. For details, see the [official JSON docs for type](https://json-schema.org/draft/2020-12/json-schema-validation#name-type). |
| `required` | array of strings | Fields that must appear in the schema. For details, see the [official JSON docs for required](https://json-schema.org/draft/2020-12/json-schema-validation#name-required). |
| `properties` | object | Properties for each field. Keys are property names and values are subschemas. For details, see the [official JSON docs for properties](https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.2.1). |
| `items` | document | Metadata about elements in array fields. Metadata appears as embedded subschemas. For details, see the [official JSON docs for items](https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1.2). |

This is not an exhaustive list of all possible fields. For details on additional fields, see the [official JSON Schema spec](https://json-schema.org/draft/2020-12).

## MongoDB Format Schema Object Properties

MongoDB format schema objects contain the following fields:

| Property | Data type | Description |
| --- | --- | --- |
| `bsonType` | string or array of strings | BSON (Binary Javascript Object Notation) 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](https://json-schema.org/draft/2020-12).

## Expanded Format Schema Object Properties

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

| Property | Data type | Description |
| --- | --- | --- |
| `x-bsonType` | string or array | BSON (Binary Javascript Object Notation) 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](https://json-schema.org/draft/2020-12).

## 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:

```json
[
  {
    "_id": { "$oid": "573a1390f29313caabcd6223" },
    "title": "The Poor Little Rich Girl",
    "plot": "Gwen's family is rich, but her parents ignore her and most of the serv...",
    "year": 1917,
  },
  {
    "_id": { "$oid": "573a1391f29313caabcd7616" },
    "title": "Salomè",
    "plot": "Salome, the daughter of Herodias, seduces her step-father/uncle Herod, ...",
    "year": 1922,
    "genres": [ "drama", "horror" ]
  },
  {
    "_id": { "$oid": "573a1392f29313caabcd9c1b" },
    "title": "Payment Deferred",
    "year": 1932,
  },
]
```

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.](https://www.mongodb.com/docs/compass/schema/export.md#std-label-compass-example-schema-to-copy)

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:

### Standard Export Format

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": [
    "_id",
    "title",
    "year"
  ],
  "properties": {
    "_id": {
      "$ref": "#/$defs/ObjectId"
    },
    "genres": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "plot": {
      "type": "string"
    },
    "title": {
      "type": "string"
    },
    "year": {
      "type": "integer"
    }
  },
  "$defs": {
    "ObjectId": {
      "type": "object",
      "properties": {
        "$oid": {
          "type": "string",
          "pattern": "^[0-9a-fA-F]{24}$"
        }
      },
      "required": [
        "$oid"
      ],
      "additionalProperties": false
    }
  }
}
```
