> For the complete documentation index, see [llms.txt](https://docs.adiom.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.adiom.io/reference/connectors/file.md). Agents can also search and read this documentation through its [MCP server](https://docs.adiom.io/~gitbook/mcp).

# File (CSV)

The file connector writes each collection or table from a Dsync source to its own CSV file, and reads those files back into any Dsync destination. It is open source and built into dsync/dsynct. The current version was added in Dsync v0.31.0.

## An Alternative to mongoexport

mongoexport exports one MongoDB collection per run and needs an explicit field list for CSV. The file connector covers the same job for a wider range of systems:

* **Any Dsync source.** Export MongoDB, DynamoDB, Cosmos DB NoSQL, PostgreSQL and other [supported sources](https://docs.adiom.io/getting-started/what-is-supported.md) the same way.
* **Many collections per run.** Pass a list of databases or collections with `--namespace`, and each one gets its own file.
* **No field list.** The columns come from the documents. Nested documents and arrays are kept as JSON in one cell.
* **Parallel reads.** A large MongoDB collection is split into `_id` ranges that are read in parallel.
* **Any Dsync destination.** The files load back into MongoDB, Cosmos DB, PostgreSQL or any other destination, with `_id` and most types restored.

|                                | mongoexport                        | Dsync file connector                                                                               |
| ------------------------------ | ---------------------------------- | -------------------------------------------------------------------------------------------------- |
| Sources                        | MongoDB                            | Any Dsync source                                                                                   |
| Collections per run            | One                                | Many, set with `--namespace`                                                                       |
| Parallelism                    | None                               | Large MongoDB collections are read in parallel `_id` ranges; files are written one batch at a time |
| Filter with a query            | `--query`                          | MongoDB sources: `--initial-sync-query` after the MongoDB URI                                      |
| Choose fields and column order | `--fields` or `--fieldFile`        | No: the ID column first, then the document keys                                                    |
| Output                         | JSON or CSV                        | CSV                                                                                                |
| Nested documents and arrays    | Named fields in `--fields` for CSV | Kept, as Extended JSON in one cell                                                                 |
| Loads into                     | MongoDB with mongoimport           | Any Dsync destination                                                                              |

To back up a MongoDB deployment, use mongodump and mongorestore, which keep BSON types and indexes. For JSON that keeps every BSON type, use `mongoexport --jsonFormat=canonical`. The file connector writes CSV only, and CSV can't carry everything a BSON document can. See [Limitations](#limitations).

## Configuration

Give a `file://` URL as the source or the destination:

```
file:///path/to/dir
file:///path/to/file.csv
```

The connector copies data once, so run it with `--mode InitialSync`.

Connector options go directly after the `file://` URL.

| Option          | Default | Description                                                                                                      |
| --------------- | ------- | ---------------------------------------------------------------------------------------------------------------- |
| `--format`      | `csv`   | File format. Only `csv` is supported.                                                                            |
| `--delimiter`   | `,`     | CSV delimiter, one character. Files written with a custom delimiter need the same flag to be read back.          |
| `--batch-size`  | `10000` | Rows per read when the connector is a source.                                                                    |
| `--infer-types` | `true`  | When reading, infer value types instead of loading every value as a string. See [Reading Files](#reading-files). |

For example, `file:///data/export --delimiter ';'` writes semicolon-separated files.

## File Layout

### As a Destination

Point the connector at a directory. Each namespace gets its own file: `shop.orders` becomes `<dir>/shop/orders.csv`. A namespace with no database part, such as a DynamoDB table, becomes `<dir>/<table>.csv`. Writing to a directory that already holds the same namespace replaces that file.

### As a Source

Give a directory or a single `.csv` file. Dsync walks a directory recursively and turns each folder path back into a namespace, so `<dir>/shop/orders.csv` is read as `shop.orders`. A single file's namespace is its file name with no database part, so map it with `--namespace`, for example `--namespace "orders:shop.orders"`.

## Data Type

The connector reads and writes **BSON** and **JSON**. Dsync picks the type the other side supports.

## Writing Files

* The ID column comes first: `_id` for BSON sources such as MongoDB, `id` for JSON sources such as Cosmos DB NoSQL. A BSON document's own `id` field stays an ordinary column.
* The first document's other keys follow in sorted order. A key that first appears in a later document becomes a new column at the end. When the export finishes, the file is rewritten once so every row has the full header.
* ObjectIDs are written as 24-character hex, dates as ISO 8601 in UTC with milliseconds, and embedded documents, arrays and types with no plain CSV form (such as Decimal128) as relaxed Extended JSON.

For example, if `shop.orders` holds two orders, and the second has no `addr`, `createdAt` or `items` but adds a `coupon` field, `orders.csv` looks like this:

```
_id,addr,createdAt,items,status,total,coupon
6af802ef5455cb5fc3d56179,"{""city"":""Paris"",""zip"":""75001""}",2026-11-01T10:00:00.000Z,"[""a b"",""c""]",paid,42.5,
6af802ef5455cb5fc3d5617a,,,,new,7.0,WELCOME
```

## Reading Files

With `--infer-types` on (the default), numbers, booleans, ObjectIDs, ISO 8601 dates, documents and arrays come back typed, and `_id` is restored. Loading the same files into MongoDB twice replaces the matching documents instead of adding copies.

Empty cells are always left out of the document.

The reader takes the ID from the column that matches the destination's data type (`_id` for BSON, `id` for JSON) and falls back to the other one. If both columns exist, the other one is kept as a field.

## Examples

Export two MongoDB collections:

```bash
dsync --mode InitialSync --namespace "shop.orders,shop.customers" \
  mongodb://localhost:27017 file:///data/export
```

Export only some documents with a filter on the MongoDB source:

```bash
dsync --mode InitialSync --namespace "shop.orders" \
  mongodb://localhost:27017 \
  --initial-sync-query '{"status": "paid", "createdAt": {"$gte": {"$date": "2026-11-01T00:00:00Z"}}}' \
  file:///data/export-paid
```

The filter applies to every collection in `--namespace`, and a collection with no matching documents gets no file.

Export DynamoDB tables:

```bash
dsync --mode InitialSync --namespace "table1,table2" dynamodb file:///data/dynamo-export
```

Export PostgreSQL tables:

```bash
dsync --mode InitialSync --namespace "public.orders,public.customers" \
  "postgresql://user:pass@localhost:5432/shop" file:///data/pg-export
```

Load an export into another database:

```bash
dsync --mode InitialSync file:///data/export mongodb://target:27017
```

Load DynamoDB files into a MongoDB database. DynamoDB tables have no database part, so map each one:

```bash
dsync --mode InitialSync --namespace "table1:mydb.table1" \
  file:///data/dynamo-export mongodb://target:27017
```

Check that files parse before writing anywhere, by reading them into `/dev/null`:

```bash
dsync --mode InitialSync file:///data/export /dev/null --log-json
```

A file that doesn't parse shows up as an `Error listing data` line. Check the log, not the exit code: v0.31.0 exits with status 0 even when a read fails.

For how each source builds its ID, see its connector page: [MongoDB](https://docs.adiom.io/reference/connectors/mongodb.md), [DynamoDB](https://docs.adiom.io/reference/connectors/dynamodb.md), [Cosmos DB NoSQL](https://docs.adiom.io/reference/connectors/cosmos-db-nosql.md), [PostgreSQL](https://docs.adiom.io/reference/connectors/postgresql.md).

## Limitations

The connector writes CSV only for now, and a CSV round trip can't tell a null from a missing field or a numeric string from a number.

* If you need JSON in S3, the [S3 connector](https://docs.adiom.io/reference/connectors/s3.md) writes JSON array files to a bucket.
* The MongoDB source stops with an error (`mixed _id types not supported`) if a collection's smallest and largest `_id` values have different BSON types, such as ObjectIDs and strings. mongoexport has no such restriction.
* On reload, a null, a missing field and an empty string all come back as a missing field.
* With type inference on, a string that looks like a number, boolean, date, ObjectID or a JSON object or array comes back as that type, and the strings `NaN`, `Infinity` and `-Infinity` come back as doubles. That includes a string `_id` such as `"42"` and a field holding a serialized JSON payload.
* Digit strings with a leading zero stay strings, so a top-level zip code field can come back mixed: `"02134"` as a string and `75001` as an integer. Inside embedded documents such values stay strings, because those cells are parsed as JSON.
* `--infer-types=false` is all or nothing. ObjectIDs, dates, numbers, documents and arrays all load as strings, so `_id` values come back as hex strings and the reloaded documents no longer match the originals.
* 64-bit integers small enough for 32 bits come back as 32-bit integers, and a `\r\n` inside a value comes back as `\n`.
* Whole doubles keep a trailing `.0` (`7.0`), and large or very small ones use exponent notation (`1.25e+06`). Dsync reads both back to the same double; other tools will see that notation.
* When later documents add columns, the file is rewritten through a temporary copy at the end of the export, so the disk needs room for about twice the largest file. If the export stops before that rewrite, the file keeps its shorter header and won't read back. Run the export again.
* It copies data once. There's no CDC, so it can't keep two systems in sync.
