Android 上的 MongoDB [英] MongoDB on Android

查看:17
本文介绍了Android 上的 MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道 MondgoDB 在 Android 上是如何工作的.它是否在本地工作并且您稍后会复制数据?是否只能通过网络后端在线工作?

Does anyone know how does MondgoDB works on Android. Does it work locally and you the data gets replicated later? Does work only online with just a web backend?

推荐答案

我要恢复这个线程并说 MongoDB 的 Java 驱动程序目前与 Android 兼容.一些新手开发人员可能无法让他们的应用程序使用 MongoDB 的 Java 库,因此我将概述您必须做的事情(尽管在您阅读本文时所有这些都可能已经过时).

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).

转到您的应用 build.gradle 文件.在您的依赖项下添加此编译"条目(您可能需要替换版本):

Go to your app build.gradle file. Add this "compile" entry under your dependencies (you will probably have to replace the version):

dependencies {
  ...
  implementation 'org.mongodb:mongo-java-driver:3.0.3'
}

如您所见,本文中的驱动程序版本为 3.0.3.您可以通过在 http://search.maven.org 搜索mongo-java-driver"或任何相关术语来找到当前版本.

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.

如果您要连接到外部数据库,您当然需要将 INTERNET 权限添加到您的清单中.连接到一个非常简单.这是一个例子.替换用户名、密码、主机域、端口和数据库名称:

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:password@www.example.com:12345/db-name" );
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase db = mongoClient.getDatabase(uri.getDatabase());

由于这与网络相关,您需要在 AsyncTask 类中运行所有这些.

Since this is network related, you will need to run all of that in an AsyncTask class.

按照 https://www.mongodb.org/ 上的 Java 教程从这里开始应该相对简单出去了.

Following the java tutorials on https://www.mongodb.org/ should be relatively straightforward from here on out.

这篇关于Android 上的 MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆