MongoDB to Snowflake

This page provides you with instructions on how to extract data from MongoDB and load it into Snowflake. (If this manual process sounds onerous, check out Stitch, which can do all the heavy lifting for you in just a few clicks.)

What is MongoDB?

MongoDB, or just Mongo, is an open source NoSQL database that stores data in JSON format. It uses a document-oriented data model, and data fields can vary by document. MongoDB isn't tied to any specified data structure, meaning that there's no particular format or schema for data in a Mongo database.

What is Snowflake?

Snowflake is a cloud-based data warehouse that's fast, flexible, and easy to work with. It runs on Amazon Web Services EC2 and S3 instances, and separates compute and storage resources, enabling users to scale the two independently and pay only for resources used. Snowflake can natively load and optimize both structured and semi-structured data and make it available via SQL. It provides native support for JSON, Avro, XML, and Parquet data, and can provide access to the same data for multiple workgroups or workloads simultaneously with no contention roadblocks or performance degradation.

Getting data out of MongoDB

The process of pulling data out of MongoDB depends on how you've loaded data into MongoDB. In some cases, it may be impossible to extract all of your data, because NoSQL databases don't require structure (i.e. specific columns). Relational databases, such as those used for data warehouses, use a more traditional, rigid structure. You'll need to defined a structure in the relational database into which you can insert MongoDB data.

Don't stress about the confusing data structure. Lots of the data that's loaded into MongoDB is created by a computer, so it probably has a pretty predictable structure. If you can find specific fields that exist for every record, you're well on your way. Make sure these fields appear in the records of each collection you'd like to replicate from MongoDB. There are many ways to do this. The most popular method to get data from MongoDB is to use the find() command.

Sample MongoDB data

MongoDB stores and returns JSON-formatted data. Here's an example of what a response might look like to a query against the products collection.

db.products.find( { qty: { $gt: 25 } }, { _id: 0, qty: 0 } )

{ "item" : "pencil", "type" : "no.2" }
{ "item" : "bottle", "type" : "blue" }
{ "item" : "paper" }

Preparing data for Snowflake

Depending on your data structures, you may need to prepare your data before loading. Check the supported data types for Snowflake and make sure that your data maps neatly to them.

Note that you won't need to define a schema in advance when loading JSON or XML data into Snowflake.

Loading data into Snowflake

Snowflake's documentation includes a Data Loading Overview that guides you through the task of loading your data. A data loading wizard in the Snowflake web UI may be useful if you're not loading a lot of data, but for many organizations, the limitations on that tool will make it unsuitable. You can load your data with two manual steps:

  • Use the PUT command to stage files.
  • Use the COPY INTO table command to load prepared data into an awaiting table.

You can copy the data from your local drive or from Amazon S3. Snowflake lets you make a virtual warehouse that can power the insertion process.

Keeping MongoDB data up to date

Fine job! You are the proud developer of a script that moves data from MongoDB to your data warehouse. This works as a one-shot deal. It's good to think about what will happen when there is new and updated data in MongoDB.

One option that works would be to load the entire MongoDB dataset all over again. That would certainly update the data, but it's not very efficient and can also cause terribly latency.

The smartest way to get data updated from MongoDB would be to identify keys that can be used as bookmarks to store where you script left off on the last run. Fields like updated_at, modified_at, or other auto-incrementing data are useful here. With that done, you can set up your script as a cron job or continuous loop to identify new data as it appears.

Other data warehouse options

Snowflake is great, but sometimes you need to optimize for different things when you're choosing a data warehouse. Some folks choose to go with Amazon Redshift, Google BigQuery, or PostgreSQL, which are RDBMSes that use similar SQL syntax, or Panoply, which works with Redshift instances. If you're interested in seeing the relevant steps for loading data into one of these platforms, check out To Redshift, To BigQuery, To Postgres, and To Panoply.

Easier and faster alternatives

If all this sounds a bit overwhelming, don’t be alarmed. If you have all the skills necessary to go through this process, chances are building and maintaining a script like this isn’t a very high-leverage use of your time.

Thankfully, products like Stitch were built to solve this problem automatically. With just a few clicks, Stitch starts extracting your MongoDB data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Snowflake data warehouse.