Release Status Released Availability Premium
Supported Versions 2.4+ SSL Connections Unsupported
Whitelist Tables/Columns Supported/Unsupported View Replication Unsupported
Destination Incompatibilities Possible incompatibilities. Learn more.

Connecting MongoDB

In this article, we’ll walk you through connecting your MongoDB database to Stitch. You’ll need some tech expertise to complete the setup, so we recommend looping in a developer or a member of your tech team to help out if you haven’t done this before.

Connecting a MongoDB database is an eight-step process:

  1. Whitelist the Stitch IP addresses
  2. Index Replication Key fields
  3. Retrieve the Stitch Public Key *
  4. Create a Stitch Linux user *
  5. Create a database user for Stitch
  6. Enter the connection info into Stitch
  7. Define the Replication Frequency
  8. Select databases & collections to sync

* These steps are only required if you’re using an SSH tunnel to connect the database to Stitch.

Prerequisites

  • Your MongoDB server must be using Auth mode. Auth mode requires every user who connects to Mongo to have a username and password. These credentials must be validated before the user will be granted access to the database.

    Servers that use the default (no-auth) are not currently supported.

  • Your server CANNOT require SSL connections. Currently, connecting to MongoDB via SSL is not currently supported by Stitch. The only supported method is SSH tunnel.

Whitelist Stitch’s IP Addresses

For the connection to be successful, you’ll need to configure your firewall to allow access from our IP addresses. Whitelist the following IPs before continuing onto the next step:

  • 52.23.137.21/32

  • 52.204.223.208/32

  • 52.204.228.32/32

  • 52.204.230.227/32

Index Fields for Replication Keys

Before you jump into the actual setup, you should consider how the documents in your Mongo database are updated. Our Mongo integration uses Incremental Replication to replicate Mongo data, which means that only new and updated data will be replicated to your data warehouse when a sync runs. Stitch uses a field you designate - called a Replication Key - to identify new and updated data.

There are two requirements for Mongo Replication Keys:

  1. The field must be indexed. Only indexed fields will display in the Replication Key drop-down.
  2. The field must exist in the root of the document.

Additionally, there are some gotchas you should avoid when selecting Replication Keys for your Mongo integration. For an in-depth walkthrough, check out the Selecting & Changing Mongo Replication Keys doc before continuing.

Retrieve Your Public Key

If you aren’t using an SSH tunnel to connect, you can skip this step.

The Public Key is used to authorize the Stitch Linux user. If the key isn’t properly installed, Stitch will be unable to access your database.

To retrieve the key:

  1. On the Stitch Dashboard page, click the Add an Integration button.
  2. Click the MongoDB icon.
  3. When the credentials page displays, click the Encryption Type menu and select the SSH Tunnel option.
  4. The Public Key will display, along with the other SSH fields.

Create a Stitch Linux User

If you aren’t using an SSH tunnel to connect, you can skip this step.

Note that anything inside square brackets - [like this] - is something you need to define when running the commands yourself.

To create the new user, run the following commands as root on your Linux server:

adduser [stitch username] -p
mkdir /home/[stitch username]
mkdir /home/[stitch username]/.ssh

To ensure the user has access to the database, we need to import the Public Key into authorized_keys. Copy the entire key into the authorized_keys file as follows:

touch /home/[stitch username]/.ssh/authorized_keys
"< [PASTE KEY HERE] >" >> /home/[stitch username]/.ssh/authorized_keys

To finish creating the user, alter the permissions on the /home/[stitch username] directory to allow access via SSH:

chown -R [stitch username]:[stitch username] /home/[stitch username]
chmod -R 700 /home/[stitch username]/.ssh

In the next step, you’ll create a database user for Stitch.

Create a Stitch Database User

To successfully connect and replicate your Mongo data, our user requires the ability to:

  • run the listDatabases command. We require this permission so we can detect the databases available for syncing.
  • run the listIndexes command. Because Stitch will only display indexed fields as Replication Key options, we require this permission to identify fields that can be used as Replication Keys.
  • run the dbVersion command. While this isn’t mandatory, it’s beneficial for us to have access to this to troubleshoot any connection or replication issues that may arise.
  • COUNT and query on all the databases you want to sync. We require these permissions to replicate your data.

You can assign a role to our user if you like, as long as the role has the necessary permissions to perform the items listed above.

When connecting to multiple databases, you can add the user by logging into Mongo as an admin user and running the following command. This example uses createUser, but older versions may use addUser. Documentation for addUser can be found here.

Replace [database name] with the name of database where the user is authenticated, or created:

use [database name]
db.createUser( { user: "[stitch username]",
				 pwd: "[secure password here]",
				 roles: ["roles here", "if you want them"]
			   }
		)

After you’ve created the user, the next step is to enter the connection and user info into Stitch.

Enter the Connection Info into Stitch

To wrap things up, you need to enter the database connection and user info into Stitch.

  1. On the Stitch Dashboard page, click the Add an Integration button.

  2. Click the MongoDB icon.
  3. Enter a name for the integration. This is the name that will display on the for the integration; it’ll also be used to create the schema in your data warehouse.

    For example, the name “Stitch MongoDB” would create a schema called stitch_mongodb in the data warehouse. This schema is where all the tables for this integration will be stored.

  4. Fill in the connection info for the database:
    • Host: In general, this will be 127.0.0.1 (localhost), but could also be some other network address (ex: 192.68.0.1) or your server’s public IP address. Note that this must be the actual address - entering localhost into this field will cause connection issues.
    • Port: Enter the MongoDB port on your server. (27017 by default)
    • Username: Enter the Stitch MongoDB user’s username.
    • Password: Enter the password for the Stitch MongoDB user.
    • Database: Enter the name of the default database Stitch will connect to. Don’t worry: we’ll find all the databases you gave the Stitch user access to, but we need an initial database to complete the connection.

If you’re using an SSH tunnel to connect your MongoDB database to Stitch, you’ll also need to do the following:

  1. Click the Encryption Type menu and select SSH Tunnel from the dropdown.
  2. Fill in the SSH connection info for the database in these fields:
    • Remote Address: Enter the IP address or hostname of the server Stitch will SSH into.
    • SSH Port: Enter the SSH port on your server. (22 by default)
    • Username: Enter the Stitch Linux (SSH) user’s username.

Define the Replication Frequency

The Replication Frequency controls how often Stitch will attempt to replicate data from your MongoDB integration. By default the frequency is set to 30 minutes, but you can change it to better suit your needs.

Before setting the Replication Frequency, note that:

  • The more often MongoDB is set to replicate, the higher the number of replicated rows.
  • The number of rows in the source may not equal the number of rows replicated by Stitch. Tables that use Full Table Replication will result in a higher number of replicated rows.

  • If you’re using a data warehouses that doesn’t natively support nested structures, you’ll see a higher number of replicated rows due to the de-nesting Stitch performs.

To help prevent overages, we recommend setting the Replication Frequency to something less frequent - like 6 hours instead of 30 minutes. For tips on reducing your row count, check out the Reducing Your Row Count section of our Billing Guide.

After selecting a Replication Frequency, click Save Integration.

Select Databases & Collections to Sync

The last step is to select the collections you want to sync. When you sync a collection, you’ll also need to define its Replication Key.

When selecting to sync, keep in mind that:

  • Mongo data can only be synced at the collection level. When a collection is set to sync, all fields in the collection will also be set to sync by default.
  • Only Incremental Replication is supported for Mongo integrations at this time. If a collection ever requires full replication - for example, to backfill existing rows with a new field’s values - you can reset the integration’s Replication Keys. This will queue a full re-sync of the integration’s data.
  • Mongo Replication Keys require special consideration. Unlike other database integrations, Mongo Replication Keys have their own specific set of gotchas. We strongly recommend reading the Mongo Replication Keys guide before you define the Replication Keys for your collections.
  • Nested records will be de-nested if your destination doesn’t natively support nested structures. Depending on how nested records are structured, one of two things can happen:
    • Nested arrays will be de-nested into subtables, which will lead to an increase in the number of replicated rows. In this case, the count of rows in your destination will be greater than what is in your source database.
    • Nested objects will be flattened into additional columns. Some destinations limit the number of columns a table can have - should the addition of columns from a de-nested object cause a table to exceed the destination’s column limit, Stitch will be unable to successfully load the data.

      For example: Redshift tables can have a maximum of 1,600 columns. If a de-nested Mongo object causes a table to have 1,700 columns, loading the data into the destination will fail.

You can sync collections by:

  1. In the Integration Details page, click the Tables to Replicate tab.
  2. Locate a collection you want to replicate.
  3. Click the checkbox next to the object’s name. A green checkmark means the object is set to sync.
  4. If there are child objects, they’ll automatically display and you’ll be prompted to select some.
  5. After you set a collection to sync, the Collection Settings page will display.
  6. In the Collection Settings page, you’ll need to define the collection’s Replication Key.
  7. Repeat this process for every collection you want to replicate. Note that when you sync a table, by default all columns will also be set to sync.

MongoDB’s Intial Sync

After you finish setting up MongoDB, you might see its Sync Status show as Pending on either the Stitch Dashboard or in the Integration Details page.

For a new integration, a Pending status indicates that Stitch is in the process of scheduling the initial sync for the integration. This may take some time to complete.


Replicating Data from MongoDB

When you connect a database as an input, Stitch only needs read-only access to the databases, collections, and fields you want to sync. There are two processes Stitch runs during the Extraction phase of the replication process: a structure sync and a data sync.

Structure Sync

The first part of the replication process is called a structure sync. This process will detect any changes to the structure of your database. For example: a new field is added to one of the collections you’re syncing in Stitch.

Stitch runs the following queries on MongoDB databases to perform a structure sync:

  • db.getMongo().getDBNames()
  • db.getCollectionNames()

For every collection in the database - even those that aren’t set to sync - we also run the following queries:

  • db.collection.count()
  • db.collection.getIndexes()

Data Sync

The second step in the Extraction phase is called a data sync. This is where Stitch actually pulls data out of your database for replication.

For collections using Incremental Replication, Stitch runs a single query (shown below) and reads out of the associated cursor in batches. Note that the Mongo equivalent for this query looks a little different.

select field_a, field_b <,...> from collection_a
where replication_key_field >=last_bookmark_value
order by replication_key_field

Recommendations

While we make every effort to ensure the queries that Stitch runs don’t impart significant load on your databases, we still have some recommendations for guaranteeing database performance:

  • Use a replica database instead of connecting directly. We recommend using read replicas in lieu of directly connecting production databases with high availability and performance requirements.
  • Apply indexes to Replication Key columns. We restrict and order our replication queries by this field, so applying an index to the fields you’re using as Replication Keys can improve performance. Indexes are required to use Mongo fields as Replication Keys.


Questions? Feedback?

Did this article help? If you have questions or feedback, please reach out to us.