Using MongoDB For Storing and Retrieving Data For Android


Navya Sri

As a mobile app developer, we all know the importance of data and its storage. In Mobile applications, there is a need to store various kind of data like user information, location etc. MongoDB is one among those databases used for storing the desired data.
In this page, you will be going to hear more about MongoDB and using MongoDB for the Android applications.

MongoDB is an open-source, document-oriented database. It is classified as NoSQL database, which supports dynamic schemas. Here data is stored in JSON format. MongoDB is made up of collections and documents.


Group of MongoDB documents. It consists of one or more documents that can have a similar set of properties.


Document is a set key-value pairs. As mentioned earlier, these documents have a dynamic schema.

Simply to say, you can think of MongoDB collection, document as an RDBMS table and row respectively.

PRO TIP My pockets are empty. How can I afford this? No, you don’t need to take out a single penny from your pocket. It comes with an absolutely free tag :P. Let’s start using such an attractive database in your Android applications.

Now we will be going to store and retrieve the data from our MongoDB. Here we are using MongoLab to access MongoDB. Let's follow the below steps to complete our task.

Login to MongoLab and create a database and collections.

2 Get your API key.

4 Fetching the stored data from the MongoDB.

#1 Login to MongoLab and create databases and collections

We are using MongoLab to create MongoDB databases and collections. It is a cloud database service that hosts MongoDB databases.

Login to MongoLab If you don’t have an account, create an account and login. Congratulations!! You have created your own database and a collection in MongoDB.

Get your API key

Copy that API key and save it somewhere. Later you need to use this in your project.

And on the same page you can see a field “Data API access”. If it’s value is disabled, click over the button “Enable Data API access” to enable the API access. Now we are ready to proceed further.

#3 Saving the data into the MongoDB collection

Till this point, we created a Database and a collection in it. Our next step is to save the data into the MongoDB. As already discussed, data will be stored in the form of Documents, which contains the JSON format data. There are two ways to save the data in the MongoDB. We will look at both.

One way is, saving the data directly in MongoLab.

There is an option to create a database in MongoLab, as similar to that of creating a collection and Database.

The other way is doing Networking calls and save the data in the given URL. Okay… Let’s begin our coding part.

1 As a first step, create a new Project in Android Studio.

2 I would like to store First Name, Last Name and Phone number information in my MongoDB. Hence create a Class named MyContact as below that defines these data fields. It is a POJO class.

Now create another class SupportData, which can be used for having a database, collection names, your API key and the URL where you are going to save the data. Have the below code snippet.

From the above picture, you can observe that I have three edit texts for first name, last name and phone number. And also you can see one save button and one fetch button. My concern is, when I click save button, the user entered data in the edit text fields has to be stored into the MongoDB collection named “Contacts”, which I have created earlier.

Include the below code snippet for save button action in your MainActivity.class. And include AsyncTask method to perform Network call in a background thread. Have the below code into your MainActivity.class.

#4 Fetching the stored data from the MongoDB

Be patient for few more minutes, we are almost near to our destination. We are able to save the data in the MongoDB using MongoLab. Now our final task is to fetch that data from MongoDB.

Include the below button action code for fetch in you MainActivity.class.[/vc_column_text][vc_column_text]
Here I have written one more AsyncTask GetContactsAsyncTask for handling Network call that deals with fetching the data back to our application.

Now Run your project and check the output. The below given emulator output I got.

#5 Well done!! It’s time to congratulate yourself.

As promised, at the end of this page you will be able to use MongoDB in your Android applications to store and fetch the data.

Full Source Code

Download APK


Related Post

Mobile Application Development Platforms in 2022
28 Oct 2021261 Views

Top-5 Best Mobile Application Development Platform in 2022

It is not an exaggeration to say that Android is the most popular mobile OS with 85% market share and north of 5 million apps in the Google...

Android Emulators
02 Aug 2021214 Views

What are Android Emulators and How to Setup an Android Emulator?

There are a variety of reasons why you would want to run an Android emulator on your PC. Mobile app developers always look to test their app...

Android 11 App Migration
14 Jul 2021226 Views

Android 11 App Migration: Android to Android App Migration Guide

Android 11 launched last year and was released to most Android smartphones by the end of the year. Interestingly, it was one of the most awaited...