java - 如何使用 mongodb api Java 检索集合名称?
问题描述
我正在尝试在我的程序中检索集合名称我为此创建了此方法
public static LinkedList<String> getListOfCollections() {
LinkedList<String> list = new LinkedList<>();
try (MongoClient mongoClient = new MongoClient(uri)) {
MongoDatabase db = mongoClient.getDatabase("db name");
MongoIterable<String> mList = db.listCollectionNames();
for (String string : mList) { // ---- crash here ----
list.add(string);
}
} catch (MongoException | ClassCastException e) {
System.err.println("Exception occurred while insert Value using getListOfCollections ");
}
return list;
}
但由于某种原因,它不断崩溃,当我使用时它也崩溃了:
MongoCursor<String> tmp = mList.iterator();
ps-uri 正在工作,我可以插入文档
解决方案
显然我的 IP 已更改,我无法访问数据库,因此我无法检索集合(并插入数据)。
课程 - 确保您的 IP 在 MongoDB 网络访问中是正确的。顺便说一句,这种方法有效!
推荐阅读
- java - ViewPagerAdapter 尝试在空对象引用上调用虚拟方法 Context.getSystemService(java.lang.String)'
- c++ - 无法从 std::shared_ptr<_Ty> 转换为 std::shared_ptr<_Ty>
- php - 使用php格式化字符串以显示为列表
- installation - Ubuntu 16.04(Xenial Xerus)中的角度安装失败
- javascript - 我如何在vue js中引用body标签“正确的方式”
- algorithm - 段树正确但查询输出不正确
- node.js - 将 forEach() 与早期返回相结合
- oracle - 将浮点数更改保存到浮点数或 varchar2 列?
- cordova - Cordova 错误:HTTP 错误 404 为 www 检索 cordova 3.6.3 版本
- r - 闪亮的交互式 ggplot,X 轴为日期