MongoDB
MongoDB destination. Writes Nilus batch, CDC, and stream output into collections; supports replace and merge strategies only. Append is explicitly rejected.
MongoDB is a distributed NoSQL document database. Nilus writes into MongoDB collections for both batch and CDC pipelines, supporting replace (drop + reinsert) and merge (ReplaceOne upsert on declared primary keys) incremental strategies.
Requirements
Connectivity and credentials must both be in place before the pipeline can run.
Connectivity
The Nilus runtime must reach the MongoDB endpoint. For self-hosted clusters that is the configured replica-set host on TCP
27017; for Atlas / managed MongoDB it is the connection string from the cluster console.For Atlas, allowlist the runtime egress IP range under Network Access, otherwise authentication will succeed but every operation will time out.
The destination accepts both
mongodb://(host list) andmongodb+srv://(DNS SRV record) URI schemes.
Required parameters
host
Yes
-
MongoDB server hostname (or comma-separated replica-set member list).
port
No
27017
MongoDB port. Omit when using mongodb+srv://.
username
Conditional
-
Required when the cluster enforces authentication.
password
Conditional
-
Required when the cluster enforces authentication.
Query parameters
No
-
Any standard connection-string option, e.g. tls=true, retryWrites=true, authSource=admin, replicaSet=rs0. Forwarded verbatim to the driver.
Important: Do not put the database name in the URI path. The MongoDB destination derives the database from the first segment of
dest_table(<database>.<collection>); the URI's database segment, if present, is overwritten with that.
Permissions
The MongoDB user needs:
Permission to authenticate against the cluster.
Permission to read, insert, update, and delete on the target collection.
For
replace, permission to issuedelete_many({})on the target collection (which is granted by the standardreadWriterole).For
merge, the samereadWriterole is enough;ReplaceOne(upsert=True)covers both update and insert paths.
URI format
SRV-discovery variant (Atlas-style):
Sink options
dest_table
Yes
Target collection in <database>.<collection> form. The destination splits on the first . only, so collection names may contain additional dots.
incremental_strategy
Yes
One of replace or merge. append is not supported and raises Unsupported write disposition 'append' for MongoDB destination.
Incremental strategy semantics
replace
On the first batch of each run, the destination issues delete_many({}) against the target collection and then inserts documents in batches of 10,000.
merge
Uses bulk_write with ReplaceOne({primary_keys}, doc, upsert=True). Requires the manifest to declare a primary_key on the dataset; otherwise the destination raises Merge operation requires primary keys for table '<collection>'.
append
Not supported.
Sample Nilus configs
Each example below is self-contained and uses the current Nilus pipeline shape.
Batch ingestion with replace
CDC ingestion with merge (current-state mirror)
Behavior and capabilities
Compute model: the Nilus runtime drives the cluster through the official
pymongodriver; the cluster does the inserts / upserts.Object model: MongoDB databases and collections; one Nilus pipeline writes into exactly one collection.
Supported pipeline types:
batchandcdc.Bulk shape: internal batch size is fixed at
10,000documents per server round-trip; the loader batch size feeding the destination is1,000. Neither is user-configurable today.mergeruns usebulk_write(..., ordered=False)so individual document failures do not abort the whole batch.Document IDs: incoming documents may carry an
_idfield; if absent, MongoDB auto-generates one. Formerge, the primary keys you declare in the manifest are the upsert filter, not_iddirectly.Type conversions: numbers map to MongoDB int / long / double; ISO timestamps to
ISODate; booleans, strings, lists, and nested objects map naturally; complex unsupported types are stringified.
Performance considerations
replacerequires a full collection drop on every run; this is fine for small / medium reference data but expensive for collections with millions of documents and many indexes (each rebuild rehydrates all indexes).For large CDC mirrors, prefer
mergeso MongoDB only updates changed documents; build a unique index on the primary-key fields ahead of time so the upsert lookup is bounded.For high-volume append-style event streams, sink to a Lakehouse / warehouse first, then sync the materialized current-state view back to MongoDB.
Troubleshooting
Database name is required to connect to the MongoDB database.
dest_table was supplied as a bare collection name.
Use <database>.<collection> form.
Unsupported write disposition 'append' for MongoDB destination.
incremental_strategy: append was set.
Use replace or merge.
Merge operation requires primary keys for table '<collection>'.
incremental_strategy: merge is set but the dataset spec has no primary_key.
Declare a primary_key on the dataset in the manifest.
ServerSelectionTimeoutError
Atlas IP allow-list rejects the runtime egress IP, or the SRV record could not be resolved.
Add the runtime egress range to the Atlas Network Access list and confirm DNS resolution from inside the runtime.
OperationFailure: Authentication failed
Wrong username/password, wrong authSource, or the user is not provisioned on the database/collection.
Verify credentials, add authSource=admin (or the correct auth DB) to the URI query, and confirm the user has readWrite on the target database.
Duplicate-key error during merge
Documents have ambiguous primary-key values, or the primary key is not actually unique upstream.
Either deduplicate upstream or pick a primary key that is genuinely unique.
TLS handshake failures
Cluster requires TLS; URI did not opt in.
Append ?tls=true (and, for self-signed dev clusters, tlsAllowInvalidCertificates=true).
Related docs
MongoDB: companion batch source.
MongoDB: companion CDC source.
Optimize sink datasets: guidance on
incremental_strategyand dataset-shape tuning.
Last updated
Was this helpful?