Skip to content
Related Articles

Related Articles

How to Install MongoDB on Android?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 26 May, 2022

MongoDB is a NoSQL document-oriented database used to store data in high volume. Instead of using columns and rows or tables that were used in traditional relational databases like SQL, MongoDB makes use of collections and documents. MongoDB is written in C++ language. MongoDB is developed and managed by MongoDB.Inc under SSPL(Server Side Public License) and it was initially released in February 2009. It also provides official driver support for all the popular languages like C, C++, C#, and .Net, Go, Java, Node.js, Perl, PHP, Python, Motor, Ruby, Scala, Swift, etc. So, one can create an application using any of these languages. Nowadays, most tech giants like Facebook, Nokia, eBay, Adobe, Google, etc. Use MongoDB to store large amounts of data. 

In simple words, MongoDB is an open-source database management system (DBMS) that uses a document-oriented database model which supports various forms of data. MongoDB uses JSON (JavaScript Object Notation) documents with the schema.

Features of MongoDB

  • Support ad hoc queries: One can search by field, and range query and it also supports regular expression searches.
  • Indexing: One can index any field in a document with the help of indexing.
  • Load Balancing: Just because of data placed in shards.MongoDB has an automatic load balancing configuration.
  • Scalability: MongoDB is a database that provides horizontal scalability with the help of sharding. Sharding means dividing or distributing the data on multiple servers.

Prerequisites to Download and Install MongoDB on Android

  1. Basic knowledge of Mongoose, MongoDB, and NodeJS.
  2. Internet Access
  3. Termux Application 
  4. MongoDB Account

Install MongoDB on Android

To download and install MongoDB on Android follow the below steps properly.

Step 1: If you already have an account on MongoDB then login into it, Else go to the official website of MongoDB and register for a new account or sign up with your Google account. If you didn’t sign up with your Google account. Then you will have to verify your email address manually to proceed to the next step. But if you signed up with your Google account then there is no need to verify your account. Your account will be verified automatically. 

Step 2: Now, After you have successfully created an account on MongoDB, the next step is to login into your account. After you have logged in to your account you will be able to see the following screen. Here we have to create a MongoDB database which will be connected to our android device later. 

So to create a database, Go to your projects folder and click on Create new project button. After clicking on creating a new project, the Next step is to give a proper name to your project in my case it’s GeeksForGeeks. Now, After giving a proper name to your project, create on the Next button as shown in the image given below.

Creating-new-project

 

This will create our project and inside this project, We will create our MongoDB database. Now after clicking on the Next button the following screen will show up.  

Step 3: Click on the Build a Database button. This action will create an initial setup of our MongoDB database.

Building-a-database

 

After clicking on the Build a Database button you will be able to see the following screen will with loads of information about the cloud provider of our database, the Region where our database will be physically present, And various paid and free plans according to your server and storage need. 

Now here you will have to choose a plan according to your storage and server need, MongoDB has both free as well as paid plans which you can choose according to your needs. If you are building a complex application with which lots of users will interact on daily basis, then you can choose any of the paid plans that are available but for a small project, you can go with a free plan. In my case, I am just going to create a demo program so, I will choose a free(Shared) plan.

Note: Keep all the settings default if you are choosing a free plan

Now after choosing your plan Just click on the Create Cluster button as shown below.

Creating-cluster

 

Step 4: Now, after clicking on the create cluster button, the following page will be shown up; here, we have to create an Admin user who will have access to our database. You can give any name and password according to your choice or else you can just give a name as admin and auto-generate a secure password with the help Autogenerate Secure Password button given below. After setting up the name and password of our admin. Click on Create User Button. This will create our admin user who will have access to our database.

Note: Make sure to remember the password as we will require it later.

Creating-user

 

Step 5: Now after creating an admin user make sure to keep all the other settings default and scroll down below until the following screen appears. Now, Once you have reached this section of the page as shown in the image given below. Then click on Add My IP Address Button.

This will allow your current IP Address to access your database. You can add custom IP addresses too by clicking on Add entry Button. Now, After adding your IP Address, click on Finish and Close Button. With this, our Database has been successfully created on MongoDB. The next step is to connect with our database. 

Adding-entries-to-IP-address-list

 

Step 6: In this step, we will create a connection method for our newly created database. Through this, we can connect our android device application to our MongoDB database. So to create a connection method click on Connect button as shown in the image given below.

Connecting-device-to-database

 

Now after clicking on Connect button the new page will pop up with the option Connect your Application. Click on connect your application button and then click on the Next button. The flow will be as follows.

Connect -> Connect your application -> Next

After clicking the next button, the following screen will pop up with the connection string that will help us to connect our application to our database. Make sure to copy the given connection string and save it some were. We will need this connection string later. The connection string is as follows,

mongodb+srv://username:<password>@cluster0.v5rpj.mongodb.net/Databasename?retryWrites=true&w=majority

Now click on the close button. We have successfully created a connection path for our application which we will create on our android device. 

Connected-created

 

Step 7: Now go to the network tab under the security folder and click on the Add IP Address button as shown in the below image, After clicking that click on Allow Access from anywhere button and save it. This action will allow our database to be accessed from anywhere. Now finally, our database is ready with our connection string, Now let’s create our application through which we will access our database.

Adding-IP-address

 

Step 8: To create an application that will connect to our database. Open your Termux application installed on your android device, as mentioned at the start of this article. Type or copy-paste the following command into it and hit enter. This may take a significant amount of time depending on your internet speed so be patient.

pkg install nodejs

If there is any error while executing the above command then type or copy-pasted the following command into your Termux application, and hit enter and re-run the above command after this command. 

apt upgrade

If there are no errors, then you are good to go. This will download the node module into your android device which will help us to download the Mongoose and other packages too. 

Node-module-downloaded

 

Now after the node has been installed successfully on our android. Next is to type or copy-paste the following command into Termux.

npm install mongoose

This will install the mongoose module into your android device.

mongoose-module-installed

 

Step 9: Now, Let’s Create our application that will connect to our Mongo database. To do the same created a JavaScript file with the name db.js or any name of your choice with a .js extension. To create this file type or copy-paste the following in Termux.

touch db.js

Creating-application

 

Now, open this db.js file with any of the text editors already present on your android device and copy-pasted the code given below into it. Just make sure to replace the Your connection string that is present in the code below on line no. 2 with the connection string or connection path that you got on step-6. Same as replaced in the image given below.

const mongoose = require(‘mongoose’) 

const url = ‘Your connection string’;

const connectionParams = { 

useNewUrlParser: true, 

useUnifiedTopology: true

mongoose.connect(url,connectionParams) 

 .then( () => { console.log(‘Connected to mongodb database successfully…!’)

}) 

.catch( (err) => { console.error(`${err}`);

})

Reference image on how to replace Your connection string with the string that you got on step no.6.

Replacing-connection-strings

 

Step 10: Now, let’s try to connect the application to our Mongo database and check if MongoDB is properly installed or not on our android. To do the same type or copy-paste the following command into Termux.

node db.js

This command will execute the code given above on step no.9 and will try to connect to our MongoDB. If the connection is successful then you will see the following output on the console to Termux “Connected to MongoDB database successfully…!”. As shown in the image given below.

This shows that our MongoDB has successfully installed and is up and running on our android.

MongoDB-installed

 

This was the setup required to get MongoDB up and running on an android device. If you got an error at any of the above, then make sure to follow all the above step carefully and retry it.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!