mongodb

MongoDB Installation And Setup On Android

Install and setup MongoDB on your android device

Greetings! Welcome to Codipher. I am Nasyx Rakeeb and in this article, I will guide you through a step-by-step process to install and set up MongoDB on Android in very simple steps and less amount of time. When I first tried to install MongoDB on android, I had to face a lot of errors. I came through every single YouTube video and article available online but all in vain because at last; I came to know that MongoDB doesn’t support android device’s CPU architecture. But in this article, I will show you how you can easily use the MongoDB database on your android device. So, without wasting the time, let’s dive in.

• Prerequisites To Install MongoDB On Android

> Termux App (Download latest version of Termux from here)

> Internet Access

> MongoDB Account (Sign-up here)

> Basic knowledge of Node.JS, MongoDB and Mongoose

> Patience

• Steps To Install MongoDB On Android

STEP 1) Go to the official website of MongoDB or click here and register for a new account or you can also sign up with a Google account.

NOTE: (use a valid email address for signup because MongoDB will send a verification email to the email address you provided)

STEP 2) If you didn’t sign up with a Google account. Then you will have to manually verify your email address to proceed to the next step. Go to your mailbox and activate your MongoDB account. But if you signed up with Google then there is no need to verify your account. Your account will be verified automatically.

• Answering Questions

STEP 3) Now you will see a welcome screen from MongoDB Atlas. On the welcome screen, the MongoDB atlas will ask you 3 questions. The first one will be What is your goal today? There will be four options to answer this question, now you can choose anyone you wish. In this case, we will choose to Build a new application. Because we are setting up our new application for the first time.

The second question will be What type of application are you building? again you can answer this question by choosing any of the options from the dropdown list. I will be choosing other option because it is not necessary to fill in the exact details on this page it doesn’t matter at all. If you also choose other option then it will ask you to describe what are you building? now you can leave this field blank or write some dummy text in it because as I said it really doesn’t matter what you fill in this welcome screen form.

The last question will be What is your preferred language? here in this field, you have to select that programming language in which you are going to build your application or connect to the MongoDB database, in my case I am going to choose Javascript.

Now click on the Next button on the right bottom of the screen and it will take you to the next page.

• Choosing Plan

STEP4) Now here you need to choose the plan, MongoDB atlas has both paid and free plans you can choose according to your needs. If you are not building a complex application you can choose the free plan and if you think that your application will be having a lot of users then you obviously have to choose from the paid ones. In my case, I am just going to create a hello world program so I will choose a free plan.

Also Read: How to install and create Reactjs app on Android

• Creating MongoDB Cluster

STEP 5) After selecting the plan you will see a page asking you to create a cluster, you don’t need to change anything because MongoDB will automatically choose the best options for you all you have to do on this page is just click the create cluster button but before clicking create cluster button make sure the price section is showing Free on the left bottom corner.

STEP 6) After successfully creating a cluster now you will be taken to the security quickstart page and here you need to create a username and a password. Make sure you create a username and password that’s easy to remember because we will need this username and password later when connecting to the database. I suggest you write down the username and password on any page.

After you have filled in the username and password fields click on the create user button. The second step will ask you Where would you like to connect from? it will be default set to My local environment and I prefer not to change it. Below it will ask you about IP Address and description, you have to click on Add my current IP Address and then click on create a button on the right bottom corner.

STEP 6) Now on the left sidebar you will see Network Access options click on it and you will see the IP Address that we added recently, delete it and click on Add IP Address and then click on Access from anywhere.

STEP 7) Now we have to connect our database. For this go back to cluster homepage or click on databases option which is on the top of sidebar and click on connect. Now click on Connect your application and I am going to connect to the database using NodeJS so I will select driver as NodeJS and version will by default set to the latest release. After you select the driver a connection string will be generated copy that connection string and paste it somewhere and click on the close button.

• Connecting To MongoDB Database

STEP 8) Now it’s finally time to connect to the database, and I will connect to the database using mongoose but first you need to install mongoose in your project root directory by executing the following command in Termux npm install mongoose and this will successfully install the mongoose package to your project directory. Before executing previous command make sure you have NodeJS installed. Now Create a file with db.js name or any other name but give .js extension to the file in your project directory. Now copy the below code and paste it in the db.js file.

const mongoose = require('mongoose') 

const url =  <<strong>connection string goes here</strong>>

const connectionParams = { useNewUrlParser: true, 
                         useCreateIndex: true, 
                         useUnifiedTopology: true 
} 

mongoose.connect(url,connectionParams) 
     .then( () => { console.log('Connected to database ')
     }) 
     .catch( (err) => { console.error(`${err}`);
     })

NOTE: (REPLACE <connection string goes here> WITH YOUR CONNECTION STRING THAT WE GENERATED IN STEP 7)

In the connection, string replaces the <password> with your password that we created in STEP 6.

Now open the Termux app and execute the following commands one by one:

i) pkg update

ii) pkg upgrade

iii) pkg install nodejs

Now we will run the db.js file In Termux by executing the following command node db.js and you will see Connected to the database in your console if there was an error while connecting to the database leave a comment below and I will reply as soon as possible. If the connection to the database was successful then you can take this to the next step by configuring your DB model by creating a “Collection” with various fields and connecting it with the express server. I will leave this to you to use according to your need. This was the basic setup required to get MongoDB Atlas up and running on an Android device.

• Frequently Asked Questions (FAQ’s)

> What is MongoDB?

• MongoDB is an open-source database management software

> What is Mongoose?

• Mongoose us an (ODM) Object Data Modeling for MongoDB based on NodeJS

> What is Express.JS?

• Express.JS also known as Express is an open-source backend web framework for NodeJS. Express is specially designed for developing web applications and API’s

> What is MongoDB Atlas?

• MongoDB Atlas is a completely- managed pall database that handles all the complexity of planting, managing, and healing deployments on the pall service provider of choice.

> What is MongoDB Compass?

• MongoDB Compass is an important GUI for querying, aggregating and assaying your MongoDB data in a visual terrain. Compass is free to use and source available and can be run on macOS, Windows, and Linux.

Also Read: Node.js explained in simple words

> What is MongoDB used for?

• MongoDB is the most generally used database in the development assiduity as a Document database. In document databases, the introductory conception of table and row in comparison with SQL database has been changed. Then row has been replaced by the term document which is a much more flexible and model-grounded data structure

> Which is better MongoDB or MySQL?

• MongoDB is faster than MySQL due to its capability to handle large quantities of unshaped data when it comes to speed. It uses slave replication, master replication to reuse vast quantities of unshaped data and offers the freedom to use multiple data types that are better than the severity of MySQL

> Is MongoDB SQL or NoSQL?

• MongoDB is a NoSQL database

> Best MongoDB tutorial

MongoDB – The Complete Developers Guide 2022

> MongoDB documentation

Click here to read the official documentation of MongoDB

• Conclusion

Boom! We have successfully connected to our MongoDB Atlas database with the help of NodeJS and Mongoose very easily. If you faced any errors during this process. Just let me know in the comment section below I’d like to help you.

Also Read: The complete computer networking course

Click here to read more articles on programming

45 Comments

  1. I get rhis error node:internal/modules/cjs/loader:988
    throw err;
    ^

    Error: Cannot find module ‘/data/data/com.termux/files/home/db.js’
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
    at Function.Module._load (node:internal/modules/cjs/loader:833:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47 {
    code: ‘MODULE_NOT_FOUND’,
    requireStack: []
    }

  2. Thank you for the good writeup. It in fact was a
    amusement account it. Look advanced to far added agreeable
    from you! By the way, how can we communicate?

Leave a Reply

Your email address will not be published. Required fields are marked *