Estuary

How to Integrate MongoDB with Relational Databases in Real Time (No Code)

Learn how to build a real-time pipeline from MongoDB to relational databases like PostgreSQL or MySQL using Estuary Flow. No scripts, no batch jobs. Just low-latency sync.

Blog post hero image
Share this article

Introduction: Why Integrate MongoDB with Relational Databases?

Modern organizations often rely on a hybrid data architecture — using MongoDB for flexible, high-performance document storage, and relational databases like PostgreSQL, MySQL, or SQL Server for structured analytics, transactional processing, and reporting.

This guide is for developers and data engineers who need to connect MongoDB applications to SQL-based systems without building or maintaining custom ETL scripts. Whether you’re powering dashboards, syncing microservices, or modernizing legacy infrastructure, seamless integration between MongoDB and relational databases is often critical to success.

According to a developer survey, fewer than 20% of teams use MongoDB in isolation — over 80% operate it alongside relational systems. But bridging the gap between these two models is not always straightforward.

Key Use Cases Driving Integration:

  • Real-time analytics: Combine operational data from MongoDB with SQL-based reporting tools to power live dashboards and business intelligence.
  • Microservices architectures: Applications using MongoDB often need to sync to a centralized relational system for consistency and governance.
  • Migration & modernization: Gradual transitions between NoSQL and SQL databases require bi-directional sync to ensure zero downtime and data integrity.

Integration Challenges:

  1. Data model mismatch: MongoDB stores schema-less JSON/BSON documents, while relational databases rely on fixed schemas and table structures. Flattening deeply nested documents into rows and columns is non-trivial.
  2. Schema drift: MongoDB’s flexible schema can evolve over time. Keeping the SQL destination aligned requires a system that understands changes and adapts automatically.
  3. Latency & freshness: Traditional batch ETL introduces delays. If you need real-time updates, you’ll need a CDC-based solution that can stream changes continuously.

In the next section, you’ll learn how MongoDB’s native Change Streams enable real-time data capture — and how to use that capability as the first step toward integrating MongoDB with relational databases.

Leveraging MongoDB Change Streams for Real-Time CDC

To build an efficient, real-time integration pipeline, you need a way to detect data changes instantly in MongoDB, and that’s where Change Streams come into play.

What Are MongoDB Change Streams?

Introduced in MongoDB 3.6, Change Streams allow applications to subscribe to a live stream of database operations (inserts, updates, deletes, and even DDL actions). They provide a simple, reliable API to monitor changes at the collection, database, or deployment level—without messy oplog parsing or polling. Change Streams are supported for replica sets and sharded clusters using the WiredTiger engine, and function out-of-the-box on MongoDB versions 4.0+.

How Change Streams Work

  • Watch a target: You can open a stream on a single collection, an entire database, or even the full cluster.
  • Event-driven output: Receives events like insertupdatedeletereplace, and with newer MongoDB versions, optional DDL events (createIndexes, etc.).
  • Full document lookup: You can configure the stream with { fullDocument: "updateLookup" } to fetch the entire document after an update, not just the changed fields.
  • Resumability: Each event carries a _id resume token. If your stream is interrupted, you can resume from the last event using this token.

Why This Matters for Integration

  • Real-time alerts enable low-latency sync to SQL targets
  • Event granularity supports detailed update tracking
  • Resume tokens provide reliability and failure recovery
  • Flexible scope lets you monitor collections, databases, or the whole cluster

How to Use Change Streams in Code

Example: open a Change Stream cursor in Node.js:

plaintext
const changeStream = db.collection('orders').watch(); changeStream.on('change', change => { console.log(change); // production code: emit to pipeline or materialize target });

You can also filter for specific events (e.g. operationType: "insert") or target whole databases/deployments.

How to Integrate MongoDB with Relational Databases in Real Time (Step-by-Step)

You’ll learn how to set up a real-time integration pipeline from MongoDB to your SQL database using Estuary Flow. We’ll walk through capturing data from MongoDB and syncing it to a relational system like PostgreSQL or MySQL, using native Change Data Capture (CDC) and zero manual coding.

Step 1: Capture Data from MongoDB for Integration with Relational Databases

Before you can sync MongoDB data with a relational database, you need a way to extract changes in real time, reliably and with minimal overhead. Estuary Flow makes this possible by using MongoDB Change Streams to capture inserts, updates, and deletes as they happen.

This step is the foundation of your MongoDB-to-SQL integration: it ensures that all relevant data is continuously collected from MongoDB and made ready to flow into your relational destination (e.g. PostgreSQL, MySQL, or SQL Server).

Here's how to configure the MongoDB source connector in Flow.

Step 1.1: Prepare Your Environment

Before you begin, make sure you have:

  • A running MongoDB instance (self-hosted or cloud-managed like MongoDB Atlas)
  • A MongoDB user with read access to the relevant collections
  • Your Estuary Flow account set up at dashboard.estuary.dev
  • If applicable, allowlisted Estuary IPs in your MongoDB provider’s firewall
  • If using a user with access to all databases, append ?authSource=admin to your connection string

Step 1.2: Create a MongoDB Capture in Flow

  1. Log into your Estuary Flow dashboard.
  2. Click Sources in the sidebar, then select + New Capture.
  3. Choose MongoDB as your source.
    Capture Data from MongoDB in Real-time

  4. Enter your MongoDB connection details:
    • The host address (e.g., mongodb://host:27017 or mongodb+srv://...)
    • Your MongoDB user and password
    • Optionally specify database names to discover, or leave it blank to auto-detect all

If your MongoDB is in a secure network, you can configure an SSH tunnel during this step.

Step 1.3: Select the Collections and Configure Capture Settings

Next, Flow will ask which MongoDB collections to capture and how to do it. For each one:

  1. Select the database name.
  2. Enter the collection name.
  3. Choose a capture mode:
    • Change Stream Incremental is best for real-time sync — it tracks all changes automatically.
    • Use Batch Snapshot for static collections (a full refresh on a schedule).
    • Use Batch Incremental if your collection doesn’t support change streams, and you have a cursor field (like _id or a timestamp).

If using batch modes, you'll also provide a cursor field to track document progress. Make sure this field is indexed for efficiency.

You can also optionally set how often Flow polls batch-mode collections — for example, every 10 minutes.

Step 1.4: Start the Capture

After setting everything up:

  1. Click Save & Publish.
  2. Flow will begin ingesting data:
    • It will backfill existing documents first.
    • Then it will switch to real-time capture via MongoDB Change Streams.
  3. You can monitor the pipeline status and performance in the Flow UI.

Flow automatically handles failures by storing resume tokens. If there’s a disconnection and the oplog hasn’t expired, it picks up right where it left off. If needed, it re-runs the backfill to ensure consistency.

Reliability Tips

  • MongoDB’s oplog should be configured with at least 24–48 hours of history to enable recovery after interruptions.
  • You can optionally enable changeStreamPreAndPostImages in MongoDB for richer update and delete events.
  • Estuary also supports SSH tunneling if your MongoDB instance is not publicly accessible.

That completes Step 1. Your MongoDB data is now continuously flowing into Flow collections. In the next section, you'll connect those collections to your relational database destination, completing the integration pipeline.

Step 2: Materialize MongoDB Data into a Relational Database

Now that your MongoDB data is flowing into Estuary Flow collections, the next step is to materialize that data, meaning, send it in real time to a SQL-based destination like PostgreSQL, MySQL, or SQL Server.

Estuary Flow supports direct materialization into the most popular relational databases using native, no-code connectors. This makes it easy to convert your flexible MongoDB documents into structured tables without custom ETL scripts.

Here’s how to set it up:

Step 2.1: Choose Your Destination Database

Estuary supports several relational database destinations, including:

Materialize MongoDB Data into Relational Databases

Before continuing, ensure you have:

  • A reachable host address for your SQL database
  • Credentials for a user with insert/update permissions
  • Permissions to create tables (recommended for automatic schema setup)

Step 2.2: Create a Materialization in Flow

  1. Go to your Flow dashboard.
  2. Click Destinations in the sidebar, then select + New Materialization.
  3. Choose the connector for your target database (e.g. PostgreSQL).
  4. Fill in the required connection fields:
    • Hostname (with port)
    • Username and password
    • Database name

If your database is behind a firewall or on a private network, you can configure SSH tunneling here as well.

Step 2.3: Bind MongoDB Collections to SQL Tables

Once your connection is validated:

  1. Select the Flow collections that you want to materialize (these came from the MongoDB capture step).
  2. Map each Flow collection to a destination table:
    • Flow will infer a schema from the MongoDB documents and generate a compatible SQL table.
    • If needed, you can adjust field mappings or rename columns before publishing.
  3. Click Save & Publish to deploy the materialization.

Flow will automatically:

  • Create the tables if they don’t already exist
  • Stream all new data changes into them in real time
  • Track delivery state to ensure exactly-once updates and no duplicates

Example: Materializing MongoDB "orders" Collection to PostgreSQL

Let’s say you captured a MongoDB collection called orders. You can materialize it into a PostgreSQL table called public.orders with a simple binding.

Flow will:

  • Use the _id field as the primary key
  • Flatten nested fields into columns
  • Keep the orders table updated continuously with inserts, updates, and deletes from MongoDB

No cron jobs. No scripts. Just live sync.

What Happens Behind the Scenes

  • Estuary Flow delivers each document update as a transaction to the SQL database
  • If schema changes in MongoDB, Flow detects it and alerts you (or adjusts automatically, depending on settings)
  • All operations are resumable and stateful — if anything fails, Flow retries and resumes intelligently

Move Data in Minutes - ETL,  ELT, CDC  - Real-time Data Integration

Optional: Configure via YAML Spec

If you prefer to manage your pipeline as code, you can define the materialization in YAML like this:

plaintext
materializations: myorg/postgres-orders:    endpoint:      connector:        image: ghcr.io/estuary/materialize-postgres:dev        config:          address: "postgres:5432"          database: "analytics"          user: "flow"          password: "flowpass"    bindings:      - source: myorg/orders        resource:          table: orders          schema: public

Now your integration is live: changes in MongoDB are reflected in your relational database with low latency, high reliability, and zero maintenance.

Conclusion: Real-Time MongoDB Integration with Relational Databases

Integrating MongoDB with relational databases no longer requires a patchwork of brittle ETL scripts, manual exports, or batch jobs that introduce latency and complexity. With Estuary Flow, you can build a robust, real-time data pipeline in minutes, without writing a single line of code.

Here’s what you accomplished:

  • Captured data from MongoDB using native Change Streams and backfill — ensuring historical + live data is synced
  • Configured collection-level settings with flexibility for real-time or batch sync
  • Materialized MongoDB documents into structured relational tables like PostgreSQL, MySQL, or SQL Server
  • Achieved low-latency, exactly-once delivery with schema-aware transformation — all through a visual, no-code interface

Whether you're modernizing your data stack, enabling cross-database joins, or centralizing data for analytics, Estuary Flow gives you the infrastructure to do it right, fast, reliable, and in real time.

What’s Next?

👉 Get started with Estuary Flow or read more about supported connectors.

FAQs

    It can be complex manually, especially with nested structures and schema drift. However, Estuary Flow handles document flattening, schema inference, and transformation automatically, simplifying the process and maintaining accuracy over time.
    Estuary Flow detects schema drift in MongoDB and either adapts the downstream structure or alerts you to take action, depending on your configuration. This ensures your pipeline remains consistent even as documents evolve.
    No. Estuary automatically flattens nested JSON structures when materializing into relational tables. You can also customize field mappings if needed, all without writing transformation code.
    Yes. Estuary Flow supports MongoDB Atlas and other hosted MongoDB deployments. You’ll just need to allowlist Estuary’s IP addresses and use the correct connection string with ?authSource=admin if using an admin user.

Start streaming your data for free

Build a Pipeline
Share this article

Table of Contents

Start Building For Free

About the author

Picture of Team Estuary
Team EstuaryEstuary Editorial Team

Team Estuary is a group of engineers, product experts, and data strategists building the future of real-time and batch data integration. We write to share technical insights, industry trends, and practical guides.

Related Articles

Popular Articles

Streaming Pipelines.
Simple to Deploy.
Simply Priced.
$0.50/GB of data moved + $.14/connector/hour;
50% less than competing ETL/ELT solutions;
<100ms latency on streaming sinks/sources.