MongoDB to BigQuery

This page provides you with instructions on how to extract data from MongoDB and load it into Google BigQuery. (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. Storage is done using a document-oriented data model, and data fields can vary by document. MongoDB isn't tied to any specified data structure, meaning that there is no particular format or schema for the data in a Mongo database.

What is Google BigQuery?

Google BigQuery is a data warehouse that delivers super-fast results from SQL queries, which it accomplishes using a powerful engine dubbed Dremel. With BigQuery, there's no spinning up (and down) clusters of machines as you work with your data. With all of that said, it's clear why some claim that BigQuery prioritizes querying over administration. It's super fast, and that's the reason why most folks use it.

Getting data out of MongoDB

The first step of herding your MongoDB data into your data warehouse is pulling data out of MongoDB. This, friend, is extremely difficult to accomplish. The process will depend on how you've loaded data to MongoDB over time. In some cases, it may be impossible to get all of your data in a complete and thorough way.

Our data extraction difficulties can be blamed squarely on the fact that NoSQL databases don't require structure (i.e. specific columns). Many other databases use a more traditional, rigid, relational structure. This means that a predefined structure needs to be assembled before we can insert MongoDB data into a relational database.

Don't stress about the confusing data structure. Remember that lots of the data that is loaded into MongoDB is created by a computer. Therefore there is a pretty predictable structure. If you can find some specific fields that exist for every record, you're well on your way. Make sure these fields reliably appear in the records of each collection you'd like to replicate from MongoDB.

There is a lot of 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. Take a gander at the code below as an example of what a response might look like when querying the products collection.


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

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

Loading data into Google BigQuery

Google Cloud Platform offers a helpful guide for loading data into BigQuery. You can use the bq command-line tool to upload the files to your awaiting datasets, adding the correct schema and data type information along the way. The bq load command is your friend here. You can find the syntax in the bq command-line tool quickstart guide. Iterate through this process as many times as it takes to load all of your tables into BigQuery.

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 primary 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

BigQuery is really great, but sometimes you need to optimize for different things when you're choosing a data warehouse. Some folks choose to go with Postgres or Redshift, which are two RDBMSes that use similar SQL syntax. If you're interested in seeing the relevant steps for loading this data into Postgres or Redshift, check out To Redshift and To Postgres.

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 Google BigQuery data warehouse.