如何从Java导入JSON文件到mongodb [英] How to import JSON file to mongodb from Java

查看:1610
本文介绍了如何从Java导入JSON文件到mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
    "colorsArray" : [ {
        "colorName" : "red",
        "hexValue" : "#f00"
    }, {
        "colorName" : "green",
        "hexValue" : "#0f0"
    }, {
        "colorName" : "blue",
        "hexValue" : "#00f"
    }, {
        "colorName" : "cyan",
        "hexValue" : "#0ff"
    }, {
        "colorName" : "magenta",
        "hexValue" : "#f0f"
    }, {
        "colorName" : "yellow",
        "hexValue" : "#ff0"
    }, {
        "colorName" : "black",
        "hexValue" : "#000"
    } ]
}


推荐答案

您正在寻找的是如何将文档插入mongodb。这样的事情应该有效。

What you are looking for is "how to insert a document into mongodb". Something like this should work.

db.colors.insertMany(
   [ {
        "colorName" : "red",
        "hexValue" : "#f00"
    }, {
        "colorName" : "green",
        "hexValue" : "#0f0"
    }, {
        "colorName" : "blue",
        "hexValue" : "#00f"
    }, {
        "colorName" : "cyan",
        "hexValue" : "#0ff"
    }, {
        "colorName" : "magenta",
        "hexValue" : "#f0f"
    }, {
        "colorName" : "yellow",
        "hexValue" : "#ff0"
    }, {
        "colorName" : "black",
        "hexValue" : "#000"
    } ]
)

现在你需要了解你的方式想要这样做。在java中有多种方法可以做到这一点。

Now you need to find out how you want to do this. there are multiple ways to do this in java.

如果您不熟悉使用基于文档的数据库并希望保持简单,我会考虑使用 Spring数据。但是你可以只使用mongodb java驱动程序。有很多关于如何操作的教程,如这个

If you are new to using document based databases and want to keep things easy I would look into using Spring data. However you could just the mongodb java driver. There are numerous tutorials on how to do it like this.

这篇关于如何从Java导入JSON文件到mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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