MongoDB connector
The MongoDB connector reads and writes collections using the mongodb: and
mongodb+srv: schemes. It supports all three source modes as a source, and
complete or incremental modes as a sink. The sink does not support
complete-atomic mode.
Connection strings
Section titled “Connection strings”The source.connect and sink.connect values follow the
MongoDB connection string format:
| Deployment | Pattern |
|---|---|
| Self-managed | mongodb://${MONGO_HOST}:27017/catalog |
| MongoDB Atlas | mongodb+srv://${MONGO_ATLAS_HOST}/catalog?retryWrites=true&w=majority |
The database name in the URI (catalog above) is required. Supply credentials
through the connection string from environment-managed configuration, such as
${MONGO_USER} and ${MONGO_PASSWORD}, and never commit literal values.
Source options
Section titled “Source options”| Parameter | Description |
|---|---|
source.connect |
MongoDB connection string, including the database name |
source.table |
Collection to read |
source.columns |
BSON projection applied to the collection, in JSON format, for example {_id:0,sku:1} |
source.where |
BSON query filter applied to the collection, in JSON format |
source.query |
Aggregation pipeline executed on the collection, as a JSON array |
Without source.query, ordinary reads sort by _id in ascending order. Validate
the ordering and the cost of the pipeline on the actual collection.
Sink options
Section titled “Sink options”| Parameter | Description |
|---|---|
sink.connect |
MongoDB connection string, including the database name |
sink.table |
Collection to write |
sink.columns |
Mapping from source fields to sink fields, used in examples 4 and 5 |
Field mapping
Section titled “Field mapping”- MongoDB-to-MongoDB transfers keep each source document.
- Relational source rows become documents. Field names are normalized through the primary-key normalization map when the column is a key, and lowercased otherwise.
- PostgreSQL
jsonbvalues are converted to a MongoDB document, or to a string when they are not an object. - Test nested, null, binary, and temporal fields on a disposable collection before production use.
Incremental replication
Section titled “Incremental replication”The incremental sink writes with the $merge stage. The $merge stage requires
MongoDB 4.2 or later, per the MongoDB documentation.
- The merge key is the first user-defined unique-index field of the sink
collection. If there is none, ReplicaDB falls back to
_id. - If the sink lacks the unique-index, ReplicaDB creates one when needed.
- Staged documents with a missing or null merge key cause the run to fail.
- When
_idis the merge key, every source document must carry_id. Keep_idinsource.columnsor in thesource.queryprojection. A run where_idis missing fails. - Relational sources must expose an
_idcolumn when_idis the merge key. - Incremental merge does not propagate source deletes. Deleted source documents remain in the sink.
sink.auto.createis a SQL DDL feature and is not supported for MongoDB collections.
Examples
Section titled “Examples”- Replicate all documents from a self-managed MongoDB collection to a MongoDB Atlas collection:
######################## ReplicaDB General Options ########################mode=completejobs=1fetch.size=100############################# Source Options ##############################source.connect=mongodb://${MONGO_HOST}:27017/catalogsource.table=products############################# Sink Options ################################sink.connect=mongodb+srv://${MONGO_ATLAS_HOST}/catalog?retryWrites=true&w=majoritysink.table=products- Replicate a subset of documents, using
source.queryto set a MongoDB aggregation pipeline:
######################## ReplicaDB General Options ########################mode=completejobs=1fetch.size=100############################# Source Options ##############################source.connect=mongodb://${MONGO_HOST}:27017/catalogsource.table=productssource.query=[{ $match : {startDate:{$gte:ISODate("2021-01-01T00:00:00.000Z")}} } \ ,{$sort:{startDate:1}} \ ,{$project: {_id:0, shopId:1, productId:1, price:1, startDate:1} } ]############################# Sink Options ################################sink.connect=mongodb+srv://${MONGO_ATLAS_HOST}/catalog?retryWrites=true&w=majoritysink.table=products- Replicate a table from a PostgreSQL database to a MongoDB Atlas collection, renaming columns with
source.columns:
######################## ReplicaDB General Options ########################mode=completejobs=1fetch.size=100verbose=true############################# Source Options ##############################source.connect=jdbc:postgresql://${PGHOST}:5432/postgressource.user=${PGUSER}source.password=${PGPASSWORD}source.table=t_rolessource.columns=id as "rolId", txt_code as code, txt_role_name as "roleName" \ , cod_v_usuario_creacion as "createdBy", fec_dt_creacion as "createdAt" \ , cod_v_usuario_modificacion as "updatedBy", fec_dt_modificacion as "updatedAt"############################# Sink Options ################################sink.connect=mongodb+srv://${MONGO_ATLAS_HOST}/catalog?retryWrites=true&w=majoritysink.table=roles- Replicate a MongoDB Atlas collection to a PostgreSQL table. Both
source.columnsandsink.columnsare required to match the source fields to the sink columns:
######################## ReplicaDB General Options ########################mode=completejobs=1fetch.size=100verbose=true############################# Source Options ##############################source.connect=mongodb+srv://${MONGO_ATLAS_HOST}/catalog?retryWrites=true&w=majoritysource.table=rolessource.columns={_id:0,rolId:1,roleName:1,createdBy:1,createdAt:1,updatedBy:1,updatedAt:1}source.where={createdAt:{$gte:ISODate("2021-01-01T00:00:00.000Z")}}############################# Sink Options ################################sink.connect=jdbc:postgresql://${PGHOST}:5432/postgressink.user=${PGUSER}sink.password=${PGPASSWORD}sink.table=t_rolessink.columns=id, txt_role_name, cod_v_usuario_creacion, fec_dt_creacion, cod_v_usuario_modificacion, fec_dt_modificacion- Replicate a MongoDB Atlas collection to PostgreSQL, projecting selected fields with
source.columns. The projection is optional; without it, every field of each document is read:
######################## ReplicaDB General Options ########################mode=completejobs=1fetch.size=100verbose=true############################# Source Options ##############################source.connect=mongodb+srv://${MONGO_ATLAS_HOST}/catalog?retryWrites=true&w=majoritysource.table=productssource.columns={_id:0,sku:1,name:1}############################# Sink Options ################################sink.connect=jdbc:postgresql://${PGHOST}:5432/postgressink.user=${PGUSER}sink.password=${PGPASSWORD}sink.table=products_slimsink.columns=sku, name