Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MongoDB

Reads from or writes to a MongoDB collection. database and collection are separate query params — MongoDB URIs don’t encode the database in the path the way this connector reads them (unlike PostgreSQL).

URL format

mongodb://[user:pass@]host[:port]?database=<db>&collection=<name>

Examples

Load a CSV file into a collection, one-shot:

mqb copy --drain \
  --from file:///data/customers.csv?format=csv \
  --to 'mongodb://localhost?database=app&collection=customers'

Non-destructive read of an existing collection (default source behavior — replica set required):

mqb copy --drain \
  --from 'mongodb://localhost?database=app&collection=customers' \
  --to 'postgres://user:pass@localhost/app?table=customers&auto_create_table=true'

By default (no consume given), a MongoDB source reads existing documents then watches for changes (capture_all) — pointing at a collection never claims or deletes its documents. This mode reads the oplog, so it requires a replica set (a single-node one is enough) and refuses to start without one.

Read a standalone mongod (no replica set), one-shot:

mqb copy --drain \
  --from 'mongodb://localhost?database=app&collection=customers&consume=snapshot' \
  --to file:///data/customers.jsonl

snapshot pages the collection by _id and ends the route on drain. It is non-destructive and needs no replica set, but it is not a tail and not resumable — it delivers what exists when the run starts, and rejects cursor_id.

Watch for new documents only (change stream), continuous:

mqb copy \
  --from 'mongodb://localhost?database=app&collection=orders&consume=capture_new' \
  --to kafka://kafka.local:9092?topic=orders

Key options

OptionPurpose
databaseRequired.
collectionCollection name.
consumecapture_all (default — snapshot then change stream, replica set required), capture_new (changes only, replica set required), snapshot (one-shot read, no replica set), or consumer (durable work queue — destructive).
checkpoint_storeWhere to persist the resume cursor for capture_new/capture_all.
username / passwordTake precedence over credentials embedded in url.

Full field list: reference/mongodb.md.