您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用Spring Boot将JSON数据文件导入mongodb?

如何使用Spring Boot将JSON数据文件导入mongodb?

我不知道是否有使用的解决方案,application.properties但是这是我的解决方案,用于从每行包含一个文档的文件中导入文档。

public static void importDocumentsFromJsonFile(File file) {
        //Read each line of the json file. Each file is one observation document.
        List<Document> observationDocuments = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(file.getPath()));) {
            String line;
            while ((line = br.readLine()) != null) {
                observationDocuments.add(Document.parse(line));
            }
        } catch (IOException ex) {
            ex.getMessage();
        }
        mongoTemplate.getCollection("yourCollection").insertMany(observationDocuments);
    }
mongodb 2022/1/1 18:23:12 有342人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶