> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wirekite.io/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB

> This guide explains how to configure MongoDB as a target database for Wirekite data loading and replication.

## Overview

Wirekite supports MongoDB as a target database for:

* **Schema Loading** - Create collections with optional JSON Schema validators
* **Data Loading** - Bulk load extracted data using InsertMany operations
* **Change Loading (CDC)** - Apply ongoing changes using BulkWrite operations

<Note>
  MongoDB loaders convert relational table data to BSON documents. Primary keys are mapped to MongoDB's `_id` field. Composite primary keys are stored as nested BSON documents (e.g., `{_id: {col1: val1, col2: val2}}`).
</Note>

## Prerequisites

Before configuring MongoDB as a Wirekite target, ensure the following requirements are met:

### Database Configuration

1. **Version**: MongoDB 4.x or above
2. **User Permissions**: The connection user must have:
   * `readWrite` role on the target database

### Limitations

<Warning>
  MongoDB does not support FOREIGN KEY or CHECK constraints. These are skipped during schema loading with a warning in the log.
</Warning>

***

## Schema Loader

The Schema Loader reads Wirekite's intermediate schema format (`.skt` file) and **emits two mongo-shell script files** — one with `db.<collection>.drop()` statements and one with `db.createCollection("<schema.table>")` statements. The orchestrator runs these scripts against the target during the schema-apply phase; the schema loader itself does not connect to MongoDB.

<Note>
  Collection names follow `schema.tablename` format (e.g., `public.users`). When the orchestrator applies the create script, `createCollection` is a no-op if the collection already exists.
</Note>

### Required Parameters

<ResponseField name="schemaFile" type="string" required>
  Path to the Wirekite schema file (`.skt`) generated by the Schema Extractor.
</ResponseField>

<ResponseField name="logFile" type="string" required>
  Absolute path to the log file for Schema Loader operations.
</ResponseField>

<ResponseField name="dropTableFile" type="string" required>
  Output file for `db.<collection>.drop()` statements (one line per table). The orchestrator runs this script against the target before re-creating collections, only when re-applying schema.
</ResponseField>

<ResponseField name="createTableFile" type="string" required>
  Output file for `db.createCollection("<schema.table>")` statements (one line per table). The orchestrator runs this script against the target during schema apply.
</ResponseField>

***

## Data Loader

The Data Loader reads Wirekite's intermediate data format (`.dkt` files) and loads documents into MongoDB collections using InsertMany with unordered batches for maximum throughput.

<Note>
  The Data Loader uses a 3-stage pipeline architecture (Scanner, Parsers, Writers) for high-performance parallel loading.
</Note>

### Required Parameters

<ResponseField name="dsnFile" type="string" required>
  Path to a file containing the MongoDB connection string.
</ResponseField>

<ResponseField name="inputDirectory" type="string" required>
  Directory containing data files (`.dkt`) to load.
</ResponseField>

<ResponseField name="schemaFile" type="string" required>
  Path to the Wirekite schema file for table structure information.
</ResponseField>

<ResponseField name="logFile" type="string" required>
  Absolute path to the log file for Data Loader operations.
</ResponseField>

### Optional Parameters

<ResponseField name="maxThreads" type="integer" default="10">
  Maximum number of parallel threads for loading files. Each thread loads one file at a time.
</ResponseField>

<ResponseField name="hexEncoding" type="boolean" default="false">
  Set to `true` if data was extracted using hex encoding instead of base64.
</ResponseField>

***

## Change Loader

The Change Loader applies ongoing data changes (INSERT, UPDATE, DELETE) to MongoDB collections using BulkWrite operations.

<Note>
  Updates use sparse `$set` operations, only modifying changed fields. Inserts and replaces use full document upserts.
</Note>

### Required Parameters

<ResponseField name="dsnFile" type="string" required>
  Path to a file containing the MongoDB connection string.
</ResponseField>

<ResponseField name="inputDirectory" type="string" required>
  Directory containing change files (`.ckt`) from the Change Extractor.
</ResponseField>

<ResponseField name="schemaFile" type="string" required>
  Path to the Wirekite schema file for table structure information.
</ResponseField>

<ResponseField name="logFile" type="string" required>
  Absolute path to the log file for Change Loader operations.
</ResponseField>

### Optional Parameters

<ResponseField name="hexEncoding" type="boolean" default="false">
  Set to `true` if change data was extracted using hex encoding.
</ResponseField>

<Warning>
  The Change Loader should not start until the Data Loader has successfully completed the initial full load.
</Warning>

***

## Orchestrator Configuration

When using the Wirekite Orchestrator, prefix target parameters with `target.schema.`, `target.data.`, or `target.change.` depending on the operation.

**Example orchestrator configuration for MongoDB target:**

```
# Main configuration
source=postgres
target=mongodb

# Schema loading
target.schema.schemaFile=/opt/wirekite/output/schema/wirekite_schema.skt
target.schema.dropTableFile=/opt/wirekite/output/schema/drop-collections.js
target.schema.createTableFile=/opt/wirekite/output/schema/create-collections.js
target.schema.logFile=/var/log/wirekite/schema-loader.log

# Data loading
target.data.dsnFile=/opt/wirekite/config/mongodb.dsn
target.data.inputDirectory=/opt/wirekite/output/data
target.data.schemaFile=/opt/wirekite/output/schema/wirekite_schema.skt
target.data.logFile=/var/log/wirekite/data-loader.log
target.data.maxThreads=8

# Change loading (CDC)
target.change.dsnFile=/opt/wirekite/config/mongodb.dsn
target.change.inputDirectory=/opt/wirekite/output/changes
target.change.schemaFile=/opt/wirekite/output/schema/wirekite_schema.skt
target.change.logFile=/var/log/wirekite/change-loader.log
```

For complete Orchestrator documentation, see the [Execution Guide](/run/execution).
