【问题标题】:How to retrieve collections name using mongodb api Java?如何使用 mongodb api Java 检索集合名称?
【发布时间】:2020-09-01 00:33:48
【问题描述】:

我正在尝试在我的程序中检索集合名称 我为此创建了这个方法

        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();

p.s- uri 正在工作,我能够插入文档

【问题讨论】:

标签: java mongodb iterator cursor


【解决方案1】:

显然我的 IP 已更改,我无法访问数据库,因此我无法检索集合(和插入数据)。

课程 - 确保您的 IP 在 MongoDB 网络访问中是正确的。 顺便说一句,这个方法有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    相关资源
    最近更新 更多