mkpipe-extractor-mongodb
MongoDB extractor plugin for MkPipe. Reads MongoDB collections into Spark DataFrames using the official MongoDB Spark Connector.
Documentation
For more detailed documentation, please visit the GitHub repository.
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Connection Configuration
connections:
mongo_source:
variant: mongodb
mongo_uri: "mongodb://user:password@host:27017/mydb?authSource=admin"
database: mydb
Alternatively, use individual parameters (URI is constructed automatically):
connections:
mongo_source:
variant: mongodb
host: localhost
port: 27017
database: mydb
user: myuser
password: mypassword
Table Configuration
pipelines:
- name: mongo_to_pg
source: mongo_source
destination: pg_target
tables:
- name: events
target_name: stg_events
replication_method: full
Incremental Replication
- name: events
target_name: stg_events
replication_method: incremental
iterate_column: updated_at
iterate_column_type: datetime
Multi-Column Incremental
When a collection has both createdDate and updatedDate fields, you can track changes on either column using a YAML list. mkpipe generates a $or query that leverages MongoDB index intersection (IXOR):
- name: product
target_name: stg_product
replication_method: incremental
iterate_column:
- createdDate
- updatedDate
iterate_column_type: datetime
Generated aggregation pipeline:
{"$match": {"$or": [{"createdDate": {"$gte": "<last_point>"}}, {"updatedDate": {"$gte": "<last_point>"}}]}}
Both fields should be indexed for optimal performance.
Custom Aggregation Pipeline
- name: orders
target_name: stg_orders
replication_method: full
custom_query: '[{"$match": {"status": "active"}}, {"$project": {"_id": 0}}]'
Connection-Level Extra Options
Any key-value pairs under extra are forwarded directly as Spark read options. This is useful for setting defaults that apply to all tables using this connection (e.g. a partitioner for Amazon DocumentDB).
connections:
docdb_source:
variant: mongodb
mongo_uri: "mongodb://user:pass@docdb-host:27017/mydb?authMechanism=SCRAM-SHA-1"
database: mydb
extra:
partitioner: com.mongodb.spark.sql.connector.read.partitioner.SinglePartitionPartitioner
Read Parallelism (Partitioners)
By default the MongoDB Spark Connector uses AutoBucketPartitioner, which relies on the $bucketAuto aggregation stage. This works on MongoDB but does not work on Amazon DocumentDB (which does not support $bucketAuto). For DocumentDB or when you need explicit control, configure one of the alternative partitioners below.
Available Partitioners (Spark Connector v10.x)
All class names are under the com.mongodb.spark.sql.connector.read.partitioner package.
| Class Name | partitioner value (full class path) |
Best For |
|---|---|---|
AutoBucketPartitioner |
com.mongodb.spark.sql.connector.read.partitioner.AutoBucketPartitioner |
Default — uses $bucketAuto (not supported on DocumentDB) |
SamplePartitioner |
com.mongodb.spark.sql.connector.read.partitioner.SamplePartitioner |
General-purpose large collections — uses $sample (works on DocumentDB) |
PaginateBySizePartitioner |
com.mongodb.spark.sql.connector.read.partitioner.PaginateBySizePartitioner |
When you want partitions based on data size |
PaginateIntoPartitionsPartitioner |
com.mongodb.spark.sql.connector.read.partitioner.PaginateIntoPartitionsPartitioner |
When you want a fixed number of partitions |
ShardedPartitioner |
com.mongodb.spark.sql.connector.read.partitioner.ShardedPartitioner |
Sharded MongoDB clusters |
SinglePartitionPartitioner |
com.mongodb.spark.sql.connector.read.partitioner.SinglePartitionPartitioner |
Small collections, or safest fallback for DocumentDB |
Configuration
- name: events
target_name: stg_events
replication_method: full
partitioner: com.mongodb.spark.sql.connector.read.partitioner.SamplePartitioner
partitioner_options:
partition.size: "64" # target partition size in MB (default: 64)
samples.per.partition: "10" # sample points per partition (default: 10)
PaginateIntoPartitionsPartitioner — fixed partition count
partitioner: com.mongodb.spark.sql.connector.read.partitioner.PaginateIntoPartitionsPartitioner
partitioner_options:
max.number.of.partitions: "8"
SinglePartitionPartitioner — no partitioning
partitioner: com.mongodb.spark.sql.connector.read.partitioner.SinglePartitionPartitioner
Amazon DocumentDB Compatibility
DocumentDB does not support $bucketAuto. Use one of:
SamplePartitioner— uses$sample, supported by DocumentDB. Good balance of parallelism and compatibility.SinglePartitionPartitioner— safest option, no special aggregation stages required. All data is read in a single partition; usewrite_partitionsin the loader or Spark repartitioning for downstream parallelism.
You can set the partitioner per-table (as shown above) or per-connection via extra (see Connection-Level Extra Options).
Performance Notes
- Small collections (<1M docs): partitioner overhead is not worth it — omit it or use
SinglePartitionPartitioner. - Large collections (>5M docs):
SamplePartitioneris a safe default. Expect 3–10x read speed-up when Spark has multiple cores/executors. partition.size: lower values → more, smaller partitions → more parallelism but more MongoDB connections.- The partitioner only affects the read side. Write parallelism is controlled by
write_partitionsin the loader.
All Table Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
string | required | MongoDB collection name |
target_name |
string | required | Destination table/collection name |
replication_method |
full / incremental |
full |
Replication strategy |
iterate_column |
string or list | — | Column(s) for incremental watermark. String for single column, list for multi-column OR logic |
iterate_column_type |
string | — | Type hint for watermark column |
custom_query |
string | — | MongoDB aggregation pipeline (JSON array string) |
partitioner |
string | — | MongoDB Spark partitioner class name |
partitioner_options |
map | {} |
Key-value options passed to the partitioner |
tags |
list | [] |
Tags for selective pipeline execution |
pass_on_error |
bool | false |
Skip table on error instead of failing |
Metadata
Release files for mkpipe-extractor-mongodb 0.12.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mkpipe_extractor_mongodb-0.12.0.tar.gz | 14.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mkpipe_extractor_mongodb-0.12.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.2 kB
Release files / mkpipe_extractor_mongodb-0.12.0.tar.gz
| Download URL | mkpipe_extractor_mongodb-0.12.0.tar.gz |
|---|---|
| Size | 14.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
efa72af0b6b2b6726d570d114007c4c803bcbf615bf7442364ef4f9b31a307e1
|
|
BLAKE2b-256 checksum How to use checksums |
1f5d75cfef5332bc223bb3da091ec2bc7f6c5e9daee6ca1fdd095ac6c26949f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.16
|
Release files / mkpipe_extractor_mongodb-0.12.0-py3-none-any.whl
| Download URL | mkpipe_extractor_mongodb-0.12.0-py3-none-any.whl |
|---|---|
| Size | 13.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d383b6862472528b11db09d86d7e94dedf4bb84d7a193d79593d68a25afcf8bf
|
|
BLAKE2b-256 checksum How to use checksums |
084b0579f0f52ad47d687736b135f1fd8f6c8aa5e2b5b0675bdc8799df45d97c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.16
|
Security for mkpipe-extractor-mongodb 0.12.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.