Connecting Mongodb from Mobile or Browser Based Application

Connecting MongoDB from Mobile or Browser based Application

Simpy put it's not a good Idea for your Android app to connect to MongoDB directly in the first place.

You should be thinking as if it was just another web client and implement a RESTful interface for all the data you want to exchange or update. Look at these links as a possibility or otherwise roll your own interface.

Better way to connect to MongoDB from Android App

From the options that are suggested in the question, I believe the 3rd option is the only reasonable one. Discussion below:


  1. Mongo DB Drivers

Using the mongoDB driver in Android isn't a great idea for several reasons.

According to this StackOverflow answer, the driver isn't compatible with Android out of the box.
There is someone that has forked the project on Github and made it compatible with Android, but the project hasn't been updated for over a year.

On a higher level, a database driver isn't a good way to connect to a database over a network you don't have any control over, especially from a mobile device.

It will also be harder (impossible?) to secure the database contents. Every app will have access to all the database. This might be ok if the database doesn't store any private data. Another big security risk is that you app would contain the necessary credentials to connect directly to the database, which could easily be obtained.

Also, this solution would make the Android app dependent on the database internals. Having an API would add flexibility and protect the app.

This is not a complete list, plenty of other reasons not to use a database driver in a mobile app may apply, also depending on what kind of app you are building.


  1. mLab Data API

I am not very familiar with mLab's Data API. From what I have gathered by reading their documentation it looks like it is just a simple API in case the mongo DB driver can't be used for some reason.

In this case, most of the issues from using the mongoDB driver also apply. The app you distribute will have to contain your API key, and their documentation states:

Your API key will give full access to all data within the databases belonging to your mLab account. If you distribute it to untrusted individuals, they can gain access to your account and your data.

Using this method would tightly couple your app and your database, and would fail to give adequate protection to the data.


  1. Create a Web API and consume it through Android App

A custom API is the way most apps solve this kind of situation. MongoDB's documentation has several references of existing frameworks to interact with a mongoDB database through HTTP. It is recommendable to use such a framework for robustness, security, and community support.

Developing a custom API will give you a solution better adapted to your application's needs, while retaining a greater degree of flexibility than the other options. It will require some work on the backend side, but it will be able to offer authentication and authorization, which are key to protecting the database and its contents.

If other clients (iOs/web/desktop apps, other servers...) that will use the same database are planned in the future, designing your API will also have many advantages. Developing new clients will be much easier. In this case, the effort spent on making a good API will have been a good investment.

Extra option

Stitch (also cited in another answer) looks like a good solution, unless it never comes out of beta. A lot comes out of the box and it allows some degree of customization and flexibility. Using stitch may help reduce the workload for the backend.

Hope this helps!

How can I connect to MongoDB using other device(s) accessing the locally hosted chat app?

Try binding the http server on 0.0.0.0 through server.listen(process.env.PORT || 4000, '0.0.0.0') and also in your index.html you got

var socket = io.connect('http://127.0.0.1:4000');

which should actually be your internal ip.

Accessing MongoDB through Android app. Not Working

Try this code:

MongoClientURI mongoUri  = new MongoClientURI("mongodb://Dbuser:dbpass@ds047692.mongolab.com:47692");
MongoClient mongoClient = new MongoClient(mongoUri);
DB db = mongoClient.getDB("testdb");
Set<String> collectionNames = db.getCollectionNames();

In addition, you should check this answer: https://stackoverflow.com/a/21555200/4810206.

Better way to connect to MongoDB from Android App

From the options that are suggested in the question, I believe the 3rd option is the only reasonable one. Discussion below:


  1. Mongo DB Drivers

Using the mongoDB driver in Android isn't a great idea for several reasons.

According to this StackOverflow answer, the driver isn't compatible with Android out of the box.
There is someone that has forked the project on Github and made it compatible with Android, but the project hasn't been updated for over a year.

On a higher level, a database driver isn't a good way to connect to a database over a network you don't have any control over, especially from a mobile device.

It will also be harder (impossible?) to secure the database contents. Every app will have access to all the database. This might be ok if the database doesn't store any private data. Another big security risk is that you app would contain the necessary credentials to connect directly to the database, which could easily be obtained.

Also, this solution would make the Android app dependent on the database internals. Having an API would add flexibility and protect the app.

This is not a complete list, plenty of other reasons not to use a database driver in a mobile app may apply, also depending on what kind of app you are building.


  1. mLab Data API

I am not very familiar with mLab's Data API. From what I have gathered by reading their documentation it looks like it is just a simple API in case the mongo DB driver can't be used for some reason.

In this case, most of the issues from using the mongoDB driver also apply. The app you distribute will have to contain your API key, and their documentation states:

Your API key will give full access to all data within the databases belonging to your mLab account. If you distribute it to untrusted individuals, they can gain access to your account and your data.

Using this method would tightly couple your app and your database, and would fail to give adequate protection to the data.


  1. Create a Web API and consume it through Android App

A custom API is the way most apps solve this kind of situation. MongoDB's documentation has several references of existing frameworks to interact with a mongoDB database through HTTP. It is recommendable to use such a framework for robustness, security, and community support.

Developing a custom API will give you a solution better adapted to your application's needs, while retaining a greater degree of flexibility than the other options. It will require some work on the backend side, but it will be able to offer authentication and authorization, which are key to protecting the database and its contents.

If other clients (iOs/web/desktop apps, other servers...) that will use the same database are planned in the future, designing your API will also have many advantages. Developing new clients will be much easier. In this case, the effort spent on making a good API will have been a good investment.

Extra option

Stitch (also cited in another answer) looks like a good solution, unless it never comes out of beta. A lot comes out of the box and it allows some degree of customization and flexibility. Using stitch may help reduce the workload for the backend.

Hope this helps!



Related Topics



Leave a reply



Submit