How to Query from Multiple MongoDB Databases Using MongoDB Atlas Data Federation
Rate this tutorial
Have you ever needed to make queries across databases, clusters, data centers, or even mix it with data stored in an AWS S3 blob? You probably haven't had to do all of these at once, but I'm guessing you've needed to do at least one of these at some point in your career. I'll also bet that you didn't know that this is possible (and easy) to do with ! These allow you to configure multiple remote MongoDB deployments, and enable federated queries across all the configured deployments.
MongoDB Atlas Data Federation allows you to perform queries across many MongoDB systems, including Clusters, Databases, and even AWS S3 buckets. Here's how MongoDB Atlas Data Federation works in practice.
Note: In this post, we will be demoing how to query from two separate databases. However, if you want to query data from two separate that are in the same database, I would personally recommend that you use the query. $lookup performs a left outer join to an unsharded collection in the same database to filter documents from the "joined" collection for processing. In this scenario, using a federated database instance is not necessary.
tl;dr: In this post, I will guide you through the process of creating and connecting to a virtual database in MongoDB Atlas, configuring paths to collections in two separate MongoDB databases stored in separate datacenters, and querying data from both databases using only a single query.
In order to follow along this tutorial, you need to:
Create a Virtual Database
- Click “set up manually” in the "create new federated database" dropdown in the top right corner of the UI.
Click Add Data Source on the Data Federation Configuration page, and select MongoDB Atlas Cluster. Select your first cluster, input
sample_mflixas the database and
theatersas the collection. Do this again for your second cluster and input
sample_restaurantsas the database and
restaurantsas the collection. For this tutorial, we will be analyzing restaurant data and some movie theater sample data to determine the number of theaters and restaurants in each zip code.
Repeat the steps above to connect the data for your other cluster and data source.
Next, drag these new data stores into your federated database instance and click save. It should look like this.
The next thing we are going to need to do after setting up our federated database instance is to connect to it so we can start running queries on all of our data. First, click connect in the first box on the data federation overview page.
Click Add Your Current IP Address. Enter your IP address and an optional description, then click Add IP Address. In the Create a MongoDB User step of the dialog, enter a Username and a Password for your database user. (Note: You'll use this username and password combination to access data on your cluster.)
You can run your queries any way you feel comfortable. You can use MongoDB Compass, the MongoDB Shell, connect to an application, or anything you see fit. For this demo, I'm going to be running my queries using and leveraging its feature. For more information on using this plugin, check out this on our Developer Hub.
Make sure you are using the connection string for your federated database instance and not for your individual MongoDB databases. To get the connection string for your new federated database instance, click the connect button on the MongoDB Atlas Data Federation overview page. Then click on Connect using MongoDB Compass. Copy this connection string to your clipboard. Note: You will need to add the password of the user that you authorized to access your virtual database here.
You're going to paste this connection string into the MongoDB Visual Studio Code plugin when you add a new connection.
For this query, I wanted to construct a unique aggregation that could only be used if both sample datasets were combined using federated query and MongoDB Atlas Data Federation. For this example, we will run a query to determine the number of theaters and restaurants in each zip code, by analyzing the
sample_mflix.theatersdatasets that were entered above in our clusters.
I want to make it clear that these data sources are still being stored in different MongoDB databases in completely different datacenters, but by leveraging MongoDB Atlas Data Federation, we can query all of our databases at once as if all of our data is in a single collection! The following query is only possible using federated search! How cool is that?
This outputs the zip codes with the most theaters and restaurants.
Congratulations! You just set up an Federated Database Instance that contains databases being run in different cloud providers. Then, you queried both databases using the MongoDB Aggregation pipeline by leveraging Atlas Data Federation and federated queries. This allows us to more easily run queries on data that is stored in multiple MongoDB database deployments across clusters, data centers, and even in different formats, including S3 blob storage.
Screenshot from the MongoDB Atlas Data Federation overview page showing the information for our new Virtual Database.