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.
Get your API key.
Saving the data into the MongoDB collection.
Fetching the stored data from the MongoDB.
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 https://mlab.com/login/. 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
API key is the most important element you need for doing networking calls. Don’t panic, I will guide you to find out your 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.
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.
As a first step, create a new Project in Android Studio.
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.
Here is my XML layout Design:
The layout screen in the emulator looks like below.
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.
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.
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.
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.