# Performing scans on collections

{% hint style="info" %}
**Required workspace permission:** Run collection scan
{% endhint %}

When you first connect to a [MongoDB](../../setting-up-your-database/mongodb) or [Amazon DynamoDB](../../setting-up-your-database/dynamodb) database, Tonic Structural performs a scan to determine the available fields in each collection, the field types, and how prevalent the fields are. It performs this scan at the same time as the initial sensitivity scan.

For each collection, Structural creates a hybrid document, which is a superset of all of the fields contained in the collection documents.

## Configuring the collection scan <a href="#collection-scan-configure" id="collection-scan-configure"></a>

By default, for each collection:

* The scan includes all of the documents in the collection, and continues until the scan is finished.
* Every unique path (field+data type) in the collection is added to the hybrid document.

You can change the default scan behavior. To change the scan configuration, use the following [environment settings](../../admin/environment-variables-setting). You can add these settings manually to the **Environment Settings** list on **Structural Settings**.

Note that these settings, including settings that include `MONGO` in the name, apply to both MongoDB and Amazon DynamoDB.

### Configuring how schemas are scanned

The following options control the number of documents that Structural scans in a collection.

These options allow you to limit the number of scanned documents when the additional documents do not add fields to the hybrid document.

For large homogenous collections, where all or most documents have the same structure, configuring these options can improve performance.

<table data-header-hidden><thead><tr><th width="414" valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top"><code>TONIC_DOCUMENT_SCAN_MAX_DOCS_COUNT</code></td><td valign="top">The maximum number of documents to scan for each schema in a collection.<br><br>For example, if this is 10, then Structural scans up to 10 documents, and ignores the remaining documents.<br><br>When this value is empty, Structural scans all of the documents. </td></tr><tr><td valign="top"><code>TONIC_DOCUMENT_SCAN_MAX_TIME_SECONDS</code></td><td valign="top">The maximum amount of time in seconds to scan a schema.<br><br>For example, if this is 360, then Structural scans a schema for up to 360 seconds.<br><br>When this value is empty, Structural continues the scan until it is complete.</td></tr></tbody></table>

If you set both options, then the scan completes when it reaches either limit. For example, if the maximum document count is 10 and the maximum scan time is 360 seconds, then the scan completes either after 10 documents or after 360 seconds, whichever comes first.

### Configuring how fields are collapsed in the hybrid document

Typically, the number of unique fields in a collection is small relative to the number of documents. However, in some cases the number of fields is similar to or greater than the number of documents.  This most commonly occurs when documents have "data as keys", such as keys that are ObjectIds, UUIDs, or incrementing integers.

In these cases, adding every unique field to the hybrid document can result in a large hybrid document that has an undesirable structure.

Structural offers configuration options to "collapse" fields within the hybrid document. This shrinks the size of the hybrid document. It also allows you to assign a generator to the collapsed group instead of to each unique key.

By default, Structural does not collapse fields.

#### Collapsing fields when the key is an ObjectId <a href="#collection-scan-config-collapse-objectid" id="collection-scan-config-collapse-objectid"></a>

To enable this, set the [environment setting](../../admin/environment-variables-setting) `TONIC_MONGO_OBJECT_ID_COLLAPSE_THRESHOLD` to the number of ObjectId keys that an object can contain before Structural collapses the object schema into a single key.

For example, if this is 10, then any object that has 10 or more ObjectId keys is collapsed into a single key.

A negative value indicates to not collapse the keys.

The default value is -1.

#### Collapsing fields when the key matches a custom pattern <a href="#collection-scan-config-collapse-regex" id="collection-scan-config-collapse-regex"></a>

To enable Structural to collapse fields, you provide a regular expression to identify the fields that can be collapsed into the same field. You then configure the number of matches that must exist before Structural collapses the fields.

To configure how the fields are collapsed, use the following [environment settings](../../admin/environment-variables-setting):

<table data-header-hidden><thead><tr><th width="407" valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top"><code>TONIC_DOCUMENT_COLLAPSE_FIELDS_REGEX</code></td><td valign="top">The regular expression that identifies the fields that can be collapsed into a single field.<br><br>By default, this value is empty.</td></tr><tr><td valign="top"><code>TONIC_DOCUMENT_COLLAPSE_FIELDS_REGEX_THRESHOLD</code></td><td valign="top">The number of fields that match the regular expression before Structural collapses the fields into a single field.<br><br>For example, if this is 5, then after Structural finds 5 fields that match the regular expression, it collapses all of the matching fields into a single field.<br><br>A negative value indicates to not collapse the fields.<br><br>The default value is -1.</td></tr></tbody></table>

For example:

* To collapse keys that are integer values, use the regular expression `[0-9]+` or `\d+`
* To collapse keys that are UUIDs, use the regular expression `[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`

## Viewing the most recent scans for each collection <a href="#collection-scan-list" id="collection-scan-list"></a>

On **Privacy Hub**, the **Latest Collection Scan** table shows the most recent scans on each scanned collection.

The **Build Schema** option runs a new scan on the collection.

<figure><img src="https://3378426797-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LSQCLFQ4bslJ-HYc8c3%2Fuploads%2FwVkeXoxQUraBsXcU14l0%2FMongoDBPrivacyHubLatestCollectionScan.png?alt=media&#x26;token=bae1e487-fef1-4f7a-b867-0bd3304f1711" alt=""><figcaption><p>Latest Collection Scan on Privacy Hub</p></figcaption></figure>

## Starting a collection scan <a href="#collection-scan-start" id="collection-scan-start"></a>

When the source database has a new collection, then on **Collection View**, you are prompted to run a scan either on that collection or on all collections.

<figure><img src="https://3378426797-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LSQCLFQ4bslJ-HYc8c3%2Fuploads%2F4m2WypZ9umaFIKVmDHJS%2FMongoDBCollectionViewScanCollection.png?alt=media&#x26;token=12c057d7-4247-42d8-8faf-376820ea770a" alt=""><figcaption><p>Collection scan prompt on Collection View</p></figcaption></figure>
