Java – How to connect Android App to MongoDB deployed on Google Cloud

androidgoogle-app-enginejavamongodb

Currently I am working on a simple app which will be able to connect to MongoDB which is deployed in Google Cloud. I have set it up MongoDB ocaly on my computer and I have made a little JAVA console app which connects to local MongoDB an I am able to do certain tasks like creating new collections, databases, retrievieng collections, deleting databases and collections, everything is working locally.

Problem which I have is that I want to connect Android App to the MongoDB which is deployed on Google Cloud. I have read on the web that I need to use some REST API to this task such as Crest Node.js but in other resource their saying it is impossible to connect like that. I am a bit confused, can you please explain me what I need to do make a sucessfull connection to the MongoDB from Android?

Edit@
What I have found out that one on Google Cloud there is only database created, there is no any virtual instance of Linux based system deployed so do I have to create a new Linux instance, install mongoDB in this instance and also a REST API and then make a connection with the server?

Best Answer

It is not a good practice to connect your Android application directly to your MongoDB database/cluster. The drivers are using TCP sockets to connect and deal with some pooling and many other things.

So as you mentioned the proper approach is to create a service layer, today REST API, that is running on your cloud infrastructure. You can deploy a Node or Java API that expose all your services to your mobile application.

In this case your Android application just call the REST API over HTTP.

You can look at this tutorial from Google that explain how to deploy a node application and mongodb database on GCE: https://developers.google.com/cloud/mean/

You can extend that to Java.

Related Topic