How to Connect MongoDB Atlas to Mobile


If you’re into tech in any way, you might have already heard of document databases and the many developments in the field. MongoDB is one of the top names in the industry, thanks to the continued increase in cloud consumption and its contributions to its growth.

MongoDB’s Atlas database management system has revolutionized how many businesses and organizations store and manage their data. Since its inception in 2009, MongoDB Atlas has powered some of the world’s largest companies and is currently at the forefront of mobile development. If you’re a developer aiming to leverage its features for Mobile, here’s a MongoDB Atlas tutorial on what you need to do:

MongoDB Atlas Basics

As a Database-as-a-Service (DBaaS), MongoDB does the setting up, deploying, and scaling of the database for you, so you can get your project up and running right away. To start using MongoDB, you’ll first need to create a MongoDB Atlas account, an organization, and a project. Once you’ve accomplished that, it’s time to set up a cluster in Atlas via the Build a Cluster button.

There are three choices to choose from; the Shared Cluster utilizes shared hardware resources and framework, while the Dedicated Cluster gives users their own set of hardware and network isolation. Third is the Multi-Cloud & Multi-Region Cluster, which offers the best availability and data replication across multiple geographic regions – for a higher price. We recommend the shared cluster if you want to start with the free tier.

Using Realm DB

In April 2019, MongoDB acquired the Realm database, making it part of the MongoDB Atlas App Services. For developing mobile applications, Realm DB is often used locally on the mobile device, while the local data and changes get synced to MongoDB Atlas on the cloud. This process is called ‘Atlas Device Sync’. We’ll now list down the steps and codes needed to connect to your mobile application.

To get the base code, clone the first repository and rename it “HelloDeviceSync”.

Enable Atlas Device Sync by updating the syncEnabled state in the Gradle file using the following:

android {
// few other things
realm {
syncEnabled = true

Then add the buildConfigField to buildTypes.

buildTypes {
debug {
buildConfigField "String", "RealmAppId", "\"App Key\""

release {
buildConfigField "String", "RealmAppId", "\"App Key\""

Creating a Studio Project

The next task is to choose an app template from Atlas App Services and link to it. It provides features such as functions, triggers, graphQL, and others, depending on your needs. You can find this in the Realm tab. Copy your App ID and input this into the build.gradle file from the previous step. You have now successfully connected MongoDB Atlas and your mobile application!

Enabling Atlas Device Sync

You’ll also be prompted to set up a username and password to authenticate your connection, but you can also choose anonymous authentication. Select the Authentication tab and enable Authentication Providers. Click review draft and deploy. Next, go to the Sync tab and choose Define Data Models. In Development Mode, select the cluster to sync, choose a partition key, and configure your database name.

Update Your Application Code

Lastly, we’ll return to your mobile application. Obtain a user instance by signing in anonymously, and obtain a realm instance using SyncConfiguration.Builder. This involves updating the creation of the view model and updating the view model constructor. For more details on this step, look for more resources on Atlas Device Sync partitioning strategies online. You can now view your results in MongoDB Atlas in collections under Realm Cluster.


We love multi-device features here at TheMobileHunt. That’s why for applications such as web and mobile development, analytics, high-volume transactions, and the like, MongoDB has become crucial in providing critical data processes.

Also Read: How to Skip YouTube Ads on TV?

Photo of author
We're a passionate team of tech enthusiasts, ever eager to assist others with their tech-related questions. Through this guide, we've empowered thousands of users to resolve their tech issues and offered insights and hacks for a wide range of gadgets.

Leave a Comment