MongoDB on Android
I'm going to revive this thread and say that MongoDB's Java driver IS currently compatible with Android. Some novice developers might have trouble getting their apps to use MongoDB's java library, so I'll just outline what you have to do (though all of this could be obsolete by the time you're reading this).
Go to your app build.gradle file. Add this "compile" entry under your dependencies (you will probably have to replace the version):
As you can see, the driver's version as of this post is 3.0.3. You can find the current version by searching "mongo-java-driver" or any related terms at http://search.maven.org.
If you're connecting to an external database, you will of course need to add the INTERNET permission to your manifest. Connecting to one is pretty simple. Here's an example. Replace the username, password, host domain, port, and database name:
MongoClientURI uri = new MongoClientURI( "mongodb://username:email@example.com:12345/db-name" );
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase db = mongoClient.getDatabase(uri.getDatabase());
Since this is network related, you will need to run all of that in an AsyncTask class.
Following the java tutorials on https://www.mongodb.org/ should be relatively straightforward from here on out.
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:
- 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.
- 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.
- 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.
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 Do I connect a local MongoDB database to my Android Studio Application without MongoLab?
The best way to connect a mongo db database to an Android app is via API REST. You can check about that here. Now, if you want to connect your local mongo db with your app is a little bit different process. MLab is a way to deploy a mongo db into the cloud and make it public so any app could connect with that, for that reason all the answers go for that way. One solution could be expose your localhost via ngrok, it's not the best way you dont do that for production it could be useful just for demo or something little.
On resume : Build a rest api with the language of your preference it not affect the result because you always return JSON, then expose your localhost rest api with ngrok and finally connect your rest api with your android app, your base url is the url the ngrok return to you.
Hope this could help. Cheers.