java - 保存后如何将 MongoDB 集合加载到 Java 程序数据结构上?

标签 java arrays database mongodb nosql

我开始使用 monogDB 在我的 java 程序中保存和加载文件,但我在将数据库加载回我的程序时遇到问题。当我通过 Mongo Compass 检查时,我的保存方法完全有效并创建了 Mongo 数据库,我的主要问题是我希望它将这些数据库值加载回我的数组中,该数组在运行代码时存储所有信息。

现在,当我关闭并打开 Java 代码并输入加载方法时,它不会检索已保存的条目


  private static void loadFile(String[] name) {
        try (MongoClient mongoClient = (MongoClient) MongoClients.create(System.getProperty("mongodb.uri"))) {
            MongoDatabase sampleTrainingDB = mongoClient.getDatabase("testDB");
            MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("collection");

            // find one document with new Document
            Document document = gradesCollection.find(new Document("title", "Customer")).first();
        }
    } 

这些是存储在我的字符串数组中的值

[Jacky、Becky、Becky、空缺、Jacky、艾米、空缺、空缺、艾米、空缺、Jackie、空缺、空缺、Jackie、Jackie、Annie、空缺、空缺、Annie、空缺、Maria、空缺,空缺,玛丽亚,空缺,空缺,玛丽亚,空缺,空缺,空缺,空缺,空缺,空缺,空缺,空缺,空缺,空缺,空缺,空缺,空缺,空缺,空缺]

这是我的 MongoDB 的图片:View_Img

enter image description here

我只需要它将它加载回我的数组,以便我的程序可以记住以前的条目。

*这就是我保存文件的方式*

private static void saveFile(String[] name) {

        // Creating a Mongo client
        MongoClient mongo = new MongoClient("localhost", 27017);

        // Creating Credentials
        MongoCredential credential;
        credential = MongoCredential.createCredential("sampleUser", "trainDb",
                "password".toCharArray());
        System.out.println("Connected to the database successfully");

        //Accessing the database
        MongoDatabase database = mongo.getDatabase("testDB");

        //Creating a collection
        System.out.println("Collection created successfully");

        // Retrieving a collection
        MongoCollection<Document> collection = database.getCollection("collection");
        System.out.println("Collection myCollection selected successfully");

        for (int i = 0; i < 42; i++) {
            if (!name[i].equals("vacant") && (i == 0 || !name[i - 1].equals(name[i]))) {
                Document document = new Document("title", "Customer")
                        .append("Name", name[i])
                        .append("Seats", i + 1);
                collection.insertOne(document);
            }
        }
        System.out.println("Document inserted successfully");

    }

有人可以帮我吗?

最佳答案

我更新了您的 loadFile 方法来获取名称数组。

private static void loadFile(String[] name) {

    try (MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"))) {

        MongoDatabase sampleTrainingDB = mongoClient.getDatabase("testDB");
        MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("collection");

         // find one document with new Document
        Document document = gradesCollection.find(new Document("title", "Customer")).first();

        /** Code added from here to the end of the method      **/  
        /** Find all documents and get the names into an array **/

        // First we will load the documents in to a List collection
        List<Document> list = new ArrayList<>();
        gradesCollection.find(new Document("title", "Customer"))
                        .into(list);
        // From the list of we extract the name field and store in a string array
        String [] names = list.stream()
                               .map(doc -> doc.get("name"))
                               .toArray(String[]::new);
        System.out.println(Arrays.toString(names));
    }
}

请注意,我还对 MongoClient 创建方法进行了更改。

关于java - 保存后如何将 MongoDB 集合加载到 Java 程序数据结构上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60733654/

相关文章:

java - 如果有多个这样的数字,如何输出keySet中最大的数字?

python - "norm"是否等同于 "Euclidean distance"?

sql-server - 禁用数据库用户而不是 SQL Server 2008 中的登录名

node.js - 如何检查 IP 是否在给定的 IP 范围内?

java - 带有 jdbc 和散列密码的 shiro

java - 初始化数组列表的简洁方法

php - 如何回显分解数组行

php - 将标签附加到 MySQL 数据库中的一行

java - 最好的 Java/Swing 浏览器组件?

c++ - 从其他数组初始化堆数组